繁体   English   中英

XmlHttpRequest仅在Cordova Android上获得状态0

[英]XmlHttpRequest getting status 0 only with cordova android

我正在进行一项cordova项目,并尝试连接到一个宁静的api,但它会不断返回空白响应。 我假设这是与cors相关的问题,但是我无法弄清我所缺少的内容,而当我在浏览器上运行该应用程序时,所有旧的相同问题都没有对我有帮助,我可以正常使用

Status Code: 200
Pragma: no-cache
Date: Thu, 18 May 2017 17:50:41 +0000
Host: localhost:8001
X-Powered-By: PHP/7.1.0RC6
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Content-Type: application/json;charset=utf-8
Access-Control-Allow-Origin: *
Cache-Control: no-store, no-cache, must-revalidate
Connection: close
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept, 
Origin, Authorization
Expires: Thu, 19 Nov 1981 08:52:00 GMT

但是,当我在android设备上运行它时,我的状态为0,我正在使用cordova 6.5.0和android 5.1,我试图删除白名单插件并重新安装它,并将这些行添加到config.xml中

<access origin="*" />
<allow-navigation href="*" />
<allow-intent href="http://*/*" />

和CSP到我的index.html文件

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 
'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' *">

我使用此代码来调用api:

function testAPI(){
    var ajax = new XMLHttpRequest();
    ajax.open("GET","http://localhost:8001/layer/hospitals",true);
    ajax.onreadystatechange=function(){
      if(ajax.readyState == 4) {
         if (ajax.status == 200 || ajax.status == 0) {
             alert(ajax.responseText);
         }
      }
    }
    ajax.send();
}

有谁知道我想念的东西吗?

由于移动设备中不存在本地主机 ,因此需要使用以下IP地址替换本地主机

ajax.open("GET","http://your ip address:8001/layer/hospitals",true);

希望对你有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM