【问题标题】:How to fetch data from API in react-native如何在 react-native 中从 API 获取数据
【发布时间】:2016-07-11 10:09:27
【问题描述】:

大家好,我正在尝试从远程服务器获取数据并使用 react-native 在 alertbox 中返回

这是我的获取代码

_onPressButtonPOST: function(){
    fetch("http://www.example.com/endpoint", {
    method: "POST",
    body: JSON.stringify({
      key: "value",
    })
  })
  .then((response) => response.json())
  .then((responseData) => {
    Alert.alert(
              "POST Response",
              "Response Body -> " + JSON.stringify(responseData.body)
          )
  })
  .done();
  },

这是我想从我的端点获取的内容。

{
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "service",
        "_type": "service",
        "_id": "ac8c5edd-1aad-406f-b476-012c6e940c1a",
        "_score": 1,
        "_source": {
          "service_owner_id": "2",
          "service_name": "miamia",
          "service_email": "mk@gmail.com",
          "service_contact_name": "mukesh",
          "service_landline_number": "12345",
          "service_mobile_number": "1234567890",
          "service_address": "vdvml",
          "service_listingType": 1,
          "service_avg_rating": 3.5,
          "sarvice_ratingcount": 10,
          "service_serviceType": 1,
          "service_working_hour": [
            ""
          ],
          "service_subcat_ids": "2,3",
          "service_description": "hi I am mukesh kumar ",
          "service_id": "ac8c5edd-1aad-406f-b476-012c6e940c1a",
          "service_logo_url": "",
          "service_gallery_url": "",
          "service_location": {
            "lat": 19.34,
            "lon": 72.845
          },
          "service_subcategory": "CAT GER ",
          "service_review": []
        }
      }
    ]
  },
  "took": 2,
  "timed_out": false
}

但在警报框中显示未定义可能是什么问题?有人可以帮忙吗? 提前致谢。

【问题讨论】:

  • 尝试记录 responseData.body 并检查它是否有值
  • 我尝试使用 console.log(JSON.stringify(response.body)) 但找不到任何东西
  • 我的意思是这样做 .then((responseData) => console.log(responseData.body) 然后检查你的开发工具日志
  • 每当我点击按钮时,它就会出现“未定义”,未定义的数量会增加,这可能是什么原因?
  • 我认为 responseData 为空

标签: android api react-native fetch remote-server


【解决方案1】:

大家好,感谢https://www.thepolyglotdeveloper.com/2015/09/make-http-requests-in-ios-with-react-native/,我发现了错误并成功使用了 API 我刚刚用 responseData 替换了 responseData.body,因为我的服务器端代码中没有包含正文,所以它返回 undefined

这是我的示例代码

.then((response) => response.json())
  .then((responseData) => {
    Alert.alert(
              "POST Response",
              "Response Body -> " + JSON.stringify(responseData)
          )
  })
  .done();

欢迎任何疑问和询问。

【讨论】:

    【解决方案2】:

    通常另一个原因是,在 PHP 端点代码中,他们尝试获取使用 $_POST、$_GET 或 $_REQUEST 发送的参数。

    在这种情况下,请在获取操作中使用 FormData 作为正文的值,而不是 Json。

    一个例子

            var FormDataToPost = new FormData();
            FormDataToPost.append('userId', 12);
    
            fetch(url, { 
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json' 
                        },
                    body: FormDataToPost
                })
                .then((response) => response.json())
                .then((responseData) => { do something })
                .catch((error) => {
                        alert(error);
                 })
                .done();
    

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 2020-02-17
      • 1970-01-01
      • 2020-04-16
      相关资源
      最近更新 更多