我有一个应用程序,我用fetch来验证用户身份.几天前它还在工作,我什么都没改.刚从react 0.27升级到0.28,not fetch不起作用.

我已经搜索了将近两天,我已经阅读了stackoverflow中几乎所有的问题.大多数用户试图从localhost获取某些内容,当他们将其更改为实际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

Looks like the issue emerged in React Native 0.28. Solution is to "Allow Arbitrary Loads" in the info.plist file found in the ios>build folder that React creates.

如果在xcode中打开整个ios文件夹,请打开此信息.plist文件您可以创建一个新密钥来允许任意加载,它应该可以解决您的问题.

Allow Arbitrary Loads

Reactjs相关问答推荐

表格提交中的react 路由导航

无法使用#13;或br将新行设置为文本区域'"&"<>

NextJs Form不允许我输入任何输入,

react 表复选框不对任何操作做出react

如何在Reac.js中通过子组件和props 调用父组件函数?

在REACT-RUTER-DOMV6中使用通用页面包装组件有问题吗?

useCallback()是否应该用于作为prop传递给子组件的函数,即使该组件包装在memo()中?

使用experial_useFormState将参数传递给服务器操作

MUI 日期 Select 器 - 最小日期混乱

无法通过 fetch 获取数据到上下文中

console.logs 没有显示在浏览器控制台上,但显示在我的终端上

React Context API 在 Next.js 中更改路由时获取默认数据

数据表格组件需要所有行都具有唯一的ID属性.或者,您可以使用getRowId属性.

单击按钮时如何添加或删除 3d 对象

将外部 JavaScript 导入 React(通过文件)

Formik onChange() 字段挂钩覆盖显示值和 Select 机制

AWS 全栈应用程序部署教程构建失败

在 Remix 中使用 chartjs 和 react-chartjs-2 会出现错误react-chartjs-2未在您的 node_modules 中找到

如何防止 mui x-date-picker 被截断?

如何将本地视频文件上传到 Google Cloud