我有一个应用程序,我正在使用它来对用户进行身份验证。它一直工作到几天前,我没有改变任何东西。刚从 React 0.27 升级到 0.28,不获取不工作。
找了快2天了,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('Some errors occured');
console.log(err);
})
.done();
我试着做一些新项目,很简单,只是从教程中使用了一个简单的获取示例,它给出了同样的错误。我试图打开我试图连接到它的网站,通过模拟器中的浏览器,它可以工作,但似乎通过我的应用程序无法连接到任何网站/IP。它在 Chrome 控制台中给出了这个错误:
TypeError: Network request failed
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
最佳答案
您应该查看此链接:https://github.com/facebook/react-native/issues/8118
看起来问题出现在 React Native 0.28 中。解决方案是在 React 创建的 ios>build 文件夹中找到的 info.plist 文件中“允许任意加载”。
如果您在 xcode 中打开整个 ios 文件夹,然后打开此 info.plist 文件,您可以创建一个新 key 以允许任意加载,它应该可以解决您的问题。
关于javascript - React-Native 获取,网络请求失败。不使用本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38077273/