当前位置: 首页 > 知识库问答 >
问题:

在react native中处理网络请求失败

胡曾笑
2023-03-14

我在使用react native fetch api时遇到了一个问题。多次请求失败。我有一个高速连接。但很多次都失败了。这个问题发生在android和ios上。

const shoppingApi  = 'myserverlink';

async function Sendshoppinapi(data) {
    try {
        let response = await fetch(shoppingApi, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'content-type':'multipart/form-data'
            },
            body: data
        });
        let responseJson = await response.json();
        return responseJson;
    }
    catch (error) {
          Alert.alert(error.toString())
    }
}

export {Sendshoppinapi};

作为post请求发送给服务器的数据

  add_to_wishlist = (item,index) => {
        {
          let data = new FormData();
        data.append('methodName',       'add_to_wishlist');
        data.append('user_id',        global.userid)
        data.append('item_id',        this.props.navigation.state.params.itemid.toString())


        Sendshoppinapi(data).then((responseJson)=>{

          console.warn(responseJson);
          if(responseJson.responseCode == '200'){
            this.setState({fav:false})
            Alert.alert('SHOPPING','Item added to wishlist successfully.',[{text: 'OK',},],{ cancelable: false })

          }
          else{
            this.setState({fav:false})
            Alert.alert('SHOPPING','Item already .',[{text: 'OK',},],{ cancelable: false })
          }
        })}
      }

共有3个答案

金秦斩
2023-03-14

当你在Android设备上运行应用程序时,API在一台计算机上,两者都在同一个网络上,我添加了一些可能的东西来检查。我没有详细的具体解决方案,因为每个主题都有很多答案。

  • 与ngrok进行快速检查https://ngrok.com/看看这是否有效。如果是:
司国源
2023-03-14

使用带有promise的then()函数。(请求的代码段)

fetch(shoppingApi, {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'content-type':'multipart/form-data'
    },
    body: data
})
.then((resp) => {
    return resp.json()
})
.then((resp) => {
  //resp contains your json data
});
颜德馨
2023-03-14

我引用了我在另一个帖子中使用的答案——不过我添加了wait。

您可以检查呼叫的状态,以确定网络呼叫失败的原因。尝试使用fetch的ok来检查响应是否有效,例如:

.then(function(response) {
    if (!response.ok) {
        //throw error
    } else {
       //valid response
    }
})

使用等待:

 let response = await fetch(url)
 if (response.ok) return await response.json()

您还可以访问响应的状态,如:

response.status;

或者,statusText,例如:

response.statusText;

查看以下内容:

https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText

https://developer.mozilla.org/en-US/docs/Web/API/Response/status

https://www.tjvantoll.com/2015/09/13/fetch-and-errors/

 类似资料:
  • 类项目: hbm文件: 方法如下:

  • 问题内容: 当我使用(RN版本0.29.1)创建一个全新的项目并将提取内容放入公共Facebook演示电影API的render方法中时,它将引发。堆栈跟踪非常无用,我无法在chrome控制台中调试网络请求。这是我要发送的内容: 问题答案: 这里的问题是,iOS默认情况下不允许HTTP请求,仅允许HTTPS。如果要启用HTTP请求,请将其添加到您的:

  • 当我使用(RN版本0.29.1)创建一个全新的项目,并将render方法中的一个fetch放到公共facebook演示电影API中时,它会抛出一个。有一个非常无用的堆栈跟踪和我不能调试网络请求在chrome控制台。以下是我正在发送的fetch:

  • 这段代码在几个月内运行良好: 但上周,react native抛出了一个错误“网络请求失败”。我确信我们的API正在工作,因为我已经在一些在线API测试仪上测试过了。 当我没有强制使用HTTPS,只有HTTP时,它就工作了。 我们当前的反应版本是:0.41 以下是错误:

  • Mpx提供了网络请求库fetch,抹平了微信,阿里等平台请求参数及响应数据的差异;同时支持请求拦截器,请求取消等 使用说明 import mpx from '@mpxjs/core' import mpxFetch from '@mpxjs/fetch' mpx.use(mpxFetch) // 第一种访问形式 mpx.xfetch.fetch({ url: 'http://xxx.com' }

  • 我正在尝试从mocky接收一些简单的json。 反应本机提取函数: 远程调试器出错