【发布时间】:2017-08-05 08:28:33
【问题描述】:
我有一个 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.]
请求如下所示:
import request from 'superagent'
const getMarkers = (url, callback) => {
request
.get(url)
.set('Accept', 'application/json')
.end(callback)
}
getMarkers('localhost:8080/markers, this.handleMarkers)
我的 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()
})
我的项目允许“任意加载”。
当我使用 ios Simulator 时,这些请求运行良好。此外,当我下载 ngrok,在我的 express 服务器目录中运行它,并向 ngrok 提供的 https 地址发出客户端请求时,这些请求运行良好。
我的问题是:如何在不使用 ngrok 之类的东西的情况下促进这些请求?如果可能的话,我希望能够仅使用 http 和 localhost 快速开发。对于其他人可以就这个问题提供的任何见解,我将不胜感激!
【问题讨论】:
标签: ios xcode http https react-native