react native fetch 在 IOS 上随机失败

时间:2021-02-03 10:54:50

标签: javascript reactjs react-native fetch fetch-api

相同的 fetch 代码在 android 中运行良好。在 IOS 上大部分时间都可以工作,但它也会出现网络请求失败错误。

情况是这样,应用程序重定向到某个其他应用程序以完成某个进程,在这种情况下我会说处于后台模式,一旦进程完成,它就会以前台模式重定向回应用程序,所以不是在这个点它使一个 http 请求调用一个 fetch 调用。

错误:网络请求失败

预期结果 我希望像往常一样在两个平台上都能通过相同的请求。

代码示例:

const login = async (authData) => {
  console.log('fetching...');
  //Setting up the request
  //Select apropriate port 8515 | 8516
  const port = handlePortSelection(authData.username);
  const url = `http:\\xxx.xxx.xxx.xxx:${port}/api/Auth/LoginUser`;

  const headers = {'Content-Type': 'application/json; charset=UTF-8'};

  const body = JSON.stringify(authData);

  const requestOptions = {
    method: 'POST',
    headers: headers,
    body: body
  };

  try {
    //Call API
    const response = await fetch(url, requestOptions);

    const data = await response.json();

    setUserData(data);
  } catch (error) {
    console.error(error);
  }
};

知道是什么导致了 ios 上的这种随机行为,通常在 ios 13 和 14 上测试过。

React 原生版本: 0.61.5

0 个答案:

没有答案
相关问题