React-Native fetch,网络请求失败。不使用本地主机。

6 浏览
0 Comments

React-Native fetch,网络请求失败。不使用本地主机。

我有一个应用程序,我使用fetch来验证用户。直到几天前都可以正常工作,而且我没有做任何更改。只是从react 0.27升级到0.28,现在fetch不工作了。

我已经搜索了将近两天,并且阅读了Stack Overflow上的几乎所有问题。大多数用户尝试从本地主机获取一些内容,当他们将其更改为实际的IP地址时,它就可以工作。

但是我没有从本地主机获取任何内容,而且我的代码以前是可以工作的。

这是我的代码:

fetch('http://somesite.com/app/connect', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'language':'en-US',
        'Authorization': 'Bearer ' + access_token,
      },
      body: JSON.stringify({
        uid: uid,
        refresh_token: refresh_token,
        token: access_token,
        device: device_id,
        device_name: device_name,
      })
    })
.then((response) => response.json())
.then((responseData) => {
    console.log(JSON.stringify(responseData.body))
})
.catch((err)=> {
  console.log('发生了一些错误');
  console.log(err);
})
.done();

我尝试创建了一些新项目,简单地使用了一个简单的fetch示例,它给出了相同的错误。我尝试在模拟器中通过浏览器打开我要连接的网站,它可以工作,但是似乎我的应用程序无法连接到任何网站/IP。在Chrome控制台中显示以下错误:

TypeError: 网络请求失败
    at XMLHttpRequest.xhr.onerror (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:28193:8)
    at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:14591:15)
    at XMLHttpRequest.setReadyState (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:29573:6)
    at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:29431:6)
    at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:29506:52
    at RCTDeviceEventEmitter.emit (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:13428:23)
    at MessageQueue.__callFunction (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:11999:23)
    at http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:11906:8
    at guard (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:11857:1)
    at MessageQueue.callFunctionReturnFlushedQueue (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&hot=true:11905:1)

实际上,我有与此用户相同的问题:React-native network request always fails

xcode的info.plist:

info.plist from xcode

0