【发布时间】:2018-04-11 09:49:44
【问题描述】:
我正在尝试让我的 React Native 应用程序在真正的 iPhone 上运行,这样我就可以开始将该应用程序与 branch.io 集成。出于某种原因,我无法让我的应用程序向 AWS 上的后端 api 发出网络请求。当我在本地开发环境中使用 localhost:3000/auth/login/ 访问后端 api 时,我的代码在模拟器中按预期工作。以下是我努力的结果。
HTTP 类发布方法:
post(endpoint, body, auth = null) {
console.log(this.apiUrl + endpoint);
console.log(this._headers(true));
console.log(body);
return Observable.defer(() => {
return Observable.fromPromise(fetch(this.apiUrl + endpoint, {
method: 'post',
headers: this._headers(auth),
body: JSON.stringify(body)
})
.then(this._checkStatus)
.then(res => res.json()));
});
}
_headers(auth) {
let token = (auth) ? token = Config.clientId : this.accessToken;
let headers = new Headers({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
});
return headers;
}
将代码构建到我的 iOS 设备后,我尝试登录,但没有任何反应。我在我的发布请求中添加了控制台调用,以确保传递了正确的值并且这似乎是正确的。以下是我在运行调试模式时尝试登录时看到的结果。我是 React Native 的新手,所以我不确定网络选项卡是否会检测到来自真实设备的请求,但是没有任何网络调用的迹象。
从 Postman 运行完全相同的调用按预期工作,如下所示:
任何帮助将不胜感激。提前谢谢你。
【问题讨论】:
-
我认为这是因为你试图访问一个 http url。您需要在应用配置的 ATS 设置中允许应用访问 http。不知道你的 ide 如何,但你应该可以用谷歌搜索它。
-
谢谢。那行得通。我实际上之前尝试过这个,但由于某种原因它不起作用。不知道我做错了什么。肯定是开发者错误。我包括了我之前引用的堆栈页面:stackoverflow.com/questions/31216758/…
标签: ios reactjs amazon-web-services react-native