React-Native 提取,网络请求失败.不使用本地主机

React-Native fetch, Network request failed. not using localhost(React-Native 提取,网络请求失败.不使用本地主机)
本文介绍了React-Native 提取,网络请求失败.不使用本地主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我使用 fetch 对用户进行身份验证.它一直工作到几天前,我没有改变任何东西.刚刚从 react 0.27 升级到 0.28,not fetch 不起作用.

我已经搜索了将近 2 天,并且我已经阅读了 stackoverflow 中的几乎所有问题.大多数用户试图从 localhost 获取某些内容,当他们将其更改为实际 ip address 时,他们就可以正常工作了.但我没有从 localhost 获取任何东西,我的代码也可以正常工作.

这是我的代码:

fetch('http://somesite.com/app/connect', {方法:'POST',标题:{'接受': '应用程序/json','内容类型':'应用程序/json','语言':'en-US','授权':'承载' + access_token,},正文: JSON.stringify({uid:uid,刷新令牌:刷新令牌,令牌:access_token,设备:device_id,设备名称:设备名称,})}).then((响应) => response.json()).then((responseData) => {console.log(JSON.stringify(responseData.body))}).catch((错误)=> {console.log('发生了一些错误');控制台日志(错误);}).done();

我尝试制作一些新项目,简单,只是使用了一个简单的 fetch 示例来教程,它给出了同样的错误.我试图通过模拟器中的浏览器打开我试图连接到它的网站,它可以工作,但似乎通过我的应用程序无法连接到任何网站/IP.它在 chrome 控制台中给出了这个错误:

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

实际上我和这个用户有同样的问题:

I have an app, which im using fetch to authenticate user. it was working till few days ago and i haven't change anything. just upgraded from react 0.27 to 0.28, not fetch is not working.

i have searched for almost 2 days and i have read almost all questions in stackoverflow. most of users trying to fetch something from localhost, and when they change it to actual ip address , they get it to work. but im not fetching anything from localhost, also mine code used to be working.

here is my code :

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();

i tried to make some new projects, simple, just used a simple fetch example fro tutorials, it gave same error. i tried to open my website which im trying to connect to it, through browser in emulator, it works, but it seems through my, app cannot connect to any website/ip. it gives this error in chrome console :

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)

actually i have the same problem as this user here : React-native network request always fails

UPDATE : info.plist from xcode

any help will be appreciated!

解决方案

You should check out this link: 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.

If you open the entire ios folder in xcode, then open this info.plist file you can create a new key to Allow Arbitrary Loads and it should fix your issue.

这篇关于React-Native 提取,网络请求失败.不使用本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)