我一直在尝试使用nativescript创建一个Android应用程序。我正在使用fetch模块从我的服务器获取响应。当我试图从 httpbin.org/get 获得响应时,它是好的。但是当我尝试从本地服务器获得响应时,我收到网络请求失败。错误。
发送到httpbin.org/get -
return fetchModule.fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) {
console.log(r);
}, function (e) {
console.log(e);
});
发送到localhost:8000 / api -
return fetchModule.fetch("http://localhost:8000/api").then(response => { return response.text(); }).then(function (r) {
console.log(r);
}, function (e) {
console.log(e);
});
当我尝试通过请求模块从purehost.js中的localhost:8000 / api获得响应时。它运行正常。但是现在,我不知道如何使用fetch模块在nativescript中解决这个问题。
答案 0 :(得分:7)
相反,localhost使用 10.0.2.2 (如果使用AVD模拟器)
10.0.2.2 - Special alias to your host loopback interface
(i.e., 127.0.0.1 on your development machine)
对于GenyMotion模拟器,环回地址为 10.0.3.2
答案 1 :(得分:0)
模拟器 API 路径:- http://10.0.2.2:5000。就像您的 API 本地路径是 http://198.168.1:5000/api 或 http://localhost:5000/api 然后替换为 http://10.0.2.2:5000/api 并且端口地址与您的本地 API 端口相同。
路径- App_resources/Android/src/main/AndroidManifest.xml :-
android:usesCleartextTraffic="true" 添加应用标签