简体   繁体   English

CORS:对本地主机的HTTP请求被react-native拒绝

[英]CORS: http requests to localhost denied with react-native

I have an Express.js server that supports CORS headers running on port 8080, and am trying to make http requests to that port from a React Native app on an iPhone 5, but am getting the following error: 我有一个Express.js服务器,该服务器支持在端口8080上运行的CORS标头,并且正在尝试从iPhone 5上的React Native应用向该端口发出http请求,但是出现以下错误:

[Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.]

The request looks like this: 该请求看起来像这样:

import request from 'superagent'

const getMarkers = (url, callback) => {
  request
    .get(url)
    .set('Accept', 'application/json')
    .end(callback)
}

getMarkers('localhost:8080/markers, this.handleMarkers)

And my Express middleware looks like this: 我的Express中间件如下所示:

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept')
  next()
})

I have allowed 'arbitrary loads' for my project. 我已为项目允许“任意负荷”。

These requests run fine when I use the ios Simulator. 当我使用ios Simulator时,这些请求运行良好。 Also, these requests run fine when I download ngrok, run it in my express server directory, and make the client side requests to the https address provided by ngrok. 此外,当我下载ngrok,在我的快速服务器目录中运行它并向ngrok提供的https地址发出客户端请求时,这些请求运行良好。

My question is: How can I facilitate these requests without using something like ngrok? 我的问题是:如何在不使用ngrok之类的情况下满足这些要求? I'd like to be able to develop quickly using only http and localhost if possible. 如果可能的话,我希望能够仅使用http和localhost快速开发。 I'd be very grateful for any insight others can offer on this question! 非常感谢其他人在这个问题上可以提供的任何见解!

I believe this is your issue: 我相信这是您的问题:

App Transport Security is a security feature, added in iOS 9, that rejects all HTTP requests that are not sent over HTTPS. App Transport Security是iOS 9中新增的一项安全功能,它拒绝所有未通过HTTPS发送的HTTP请求。 This can result in HTTP traffic being blocked, including the developer React Native server. 这可能导致阻止HTTP通信,包括开发人员React Native服务器。

ATS is disabled by default in projects generated using the React Native CLI in order to make development easier. 默认情况下,在使用React Native CLI生成的项目中,ATS是禁用的,以便简化开发。 You should re-enable ATS prior to building your app for production by removing the NSAllowsArbitraryLoads entry from your Info.plist file in the ios/ folder. 您应该在从ios /文件夹的Info.plist文件中删除NSAllowsArbitraryLoads条目以构建用于生产的应用之前,重新启用ATS。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>**YOUR DOMAIN HERE**</key>
        <dict>
            <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

In case it's a bug, here are some work-a-rounds I have found helpful: 万一这是一个错误,我发现以下一些工作很有帮助:

For the express app: 对于快速应用:
-Try explicitly adding the http headers: "X-Requested-With": "XMLHttpRequest" -尝试显式添加http标头: "X-Requested-With": "XMLHttpRequest"
-I have sometimes needed to set my server running on IP 0.0.0.0 instead of 127.0.0.1 -有时我需要将服务器设置为在IP 0.0.0.0而不是127.0.0.1上运行

For the react-native app: 对于本机应用程序:
-I have had experiences where I needed to use 127.0.0.1, or the name of the specific host when making requests (versus using localhost). -我有需要使用127.0.0.1或发出请求时特定主机名的经验(与使用localhost相对)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM