2015-05-28 48 views
0

我不明白为什么,但我的CORS呼叫不起作用。科尔多瓦CORS呼叫不起作用

我加入了元标记的index.html

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

我的Android清单有:

​​

我的电话的标题是:

Access-Control-Allow-Headers → Content-Type 
Access-Control-Allow-Methods → GET,PUT,POST,DELETE 
Access-Control-Allow-Origin → * 

在我的配置。 xml我有

<access origin="*" /> 
<plugin name="cordova-plugin-whitelist" version="1" /> 

和我的代码是

var request = new XMLHttpRequest(); 
request.open("GET", "http://XXXXXXXXXX/XXXXX", true); 
console.log("test"); 
request.onreadystatechange = function() { 
    console.log("DOES NOT COME HERE?") 
    if (request.readyState == 4) { 
     if (request.status == 200 || request.status == 0) { 
      alert(code); 
      var product = JSON.parse(request.responseText); 
      alert(product.CNK); 
     } 
    } 

“不来这里”不打印它,在日志中还印有没有错误?我真的不知道我做错了什么:(

+0

我认为这与codova通过file:///协议加载您的index.html而不是http的事实有关。因为这个,我想你甚至不需要CORS。你在你的应用中设置了'吗?您必须将您想要调用的服务器的域列入白名单:http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html – Timo

+0

已更新的问题:) – Nick

+0

您使用的是什么Android版本?有了Android <4.4 CSP会被忽略,当CSP出现问题时,> = 4.4,这些日志解释了相当多的问题。你是用phonegap构建还是本地构建的(如果在本地构建,我不认为只是在config.xml中添加插件就足够了,它应该添加到CLI中)。您可以检查白名单插件是否实际存在于platforms/androidassets/www/plugins文件夹中。 – QuickFix

回答

1

确定的解决方案是很愚蠢的,我搜索的方式来长了

var request = new XMLHttpRequest(); 
request.open("GET", "http://XXXXXXXXXX/XXXXX", true); 
console.log("test"); 
request.send() 
request.onreadystatechange = function() { 
console.log("DOES NOT COME HERE?") 
if (request.readyState == 4) { 
    if (request.status == 200 || request.status == 0) { 
     alert(code); 
     var product = JSON.parse(request.responseText); 
     alert(product.CNK); 
    } 
} 

正如你看到的我加request.send(): )