2017-03-09 151 views
1

我工作的一个项目,让Android内置离子2版应用程序来创建用户和验证通过Django的REST API一个Django开发站点。离子的应用程序无法连接到本地主机

认证使用ionic serve这是通过使用跨源资源共享(如安装谷歌浏览器插件)取得时的作品。

由于我试图在实际的Android设备上运行应用程序,所以在使用应用程序时,身份验证失败(HTTP 404:URL未找到),但可以通过同一设备的浏览器访问本地主机(通过192.168。 22.4:80)。

详情:

我现在的私有IP地址是192.168.22.4和开发的网站目前在本地主机端口80通过WAMP Apache服务器提供服务。

这里是离子应用我的http请求(注册)的代码片段:

let headers = new Headers(); 
     headers.append('Content-Type','application/json'); 

     this.http.post("http://192.168.22.4:80/api-user-create", JSON.stringify(this.postData), {headers: headers}).subscribe(res => { 
     console.log(res.json()); 
     this.showAlert(); 
     }, (err) => { 
     console.log(err); 
     }); 

应用解决方案:

这些都是我曾经尝试,但仍无法连接到步本地主机:

  1. 确保允许该应用访问WiFi连接,验证我的IP地址是否正确,关闭防火墙a甚至我的杀毒软件。我还在我的开发服务器上启用了网络发现。
  2. 使用代理服务器(ngrok)和编辑离子请求
  3. 启动Apache服务器和编辑离子请求
  4. 编辑离子请求代码上CORS。我曾试图改变192.168.22.4:80到:
    • 192.168.22.4
    • 127.0.0.1:80
    • 127.0.0.1
    • localhost:80
    • localhost
    • 10.0.2.2
    • 10.0.2.2:80

有没有人遇到过这个问题并解决它?

回答

1

问题解决! 我已经解决了它:

  1. 使WAMP听< IP地址>:<端口号>。它最初默认设置为0.0.0.0:80。要配置的,我更新了httpd.conf我WAMP的服务器上,然后加入一行: Listen 192.168.22.4:80
  2. 我离子项目安装科尔多瓦 - 插件白名单。要做到这一点,我要运行: cordova plugin add cordova-plugin-whitelist --save
  3. 编辑我的离子代码和删除的端口号请求:

    let headers = new Headers(); 
        headers.append('Content-Type','application/json'); 
    
        this.http.post("http://192.168.22.4/api-user-create", JSON.stringify(this.postData), {headers: headers}).subscribe(res => { 
        console.log(res.json()); 
        this.showAlert(); 
        }, (err) => { 
        console.log(err); 
        }); 
    
  4. 重建并运行应用程序

Ionic App - No Internet Access了解更多信息。

相关问题