提取Api:无法从本地主机获取数据 [英] Fetch Api:Can not get data from localhost

查看:124
本文介绍了提取Api:无法从本地主机获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用nativescript创建一个android应用程序.我正在使用fetch模块从服务器获取响应.当我尝试从 httpbin.org/get 获取响应时,它是可以.但是,当我尝试从本地服务器获取响应时,出现网络请求失败.错误.

I have been trying to create an android app using nativescript.I am using fetch module to get response from my server.When I am trying to get response from httpbin.org/get ,it is OK.But when I am trying to get response from my local server,I am getting Network Request Failed. error.

发送到httpbin.org/get-

Sending to 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-

Sending to 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);
    });

当我尝试通过request模块从纯node.js中从localhost:8000/api获得响应时,它工作正常.但是现在,我不知道如何使用fetch模块在nativescript中解决此问题.

When I try to get response from localhost:8000/api in pure node.js via the request module.It worked fine.But now,I don't to know how to solve this in nativescript using fetch module.

推荐答案

相反,localhost(通常为127.0.0.1)使用 10.0.2.2 (如果使用AVD模拟器)

Instead, localhost (usually 127.0.0.1) use 10.0.2.2 (if using AVD emulator)

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

For GenyMotion emulator the loopback address is 10.0.3.2

这篇关于提取Api:无法从本地主机获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆