Cordova / phonegap&node.js:XMLHttpRequest返回状态0

我在node.js中写了一个HTTP服务器

var http = require('http'); http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World ' + request.url + '\n\n'); console.log(request.url); }).listen(8124); console.log('Server running at http://127.0.0.1:8124/'); 

这应该来自phonegap / cordova应用程序的请求:

 function testnodejs() { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Your webbrowser does not support HTTP Request"); return; } var url="http://domain.com:8124/it?hi=hi!"; console.log(url); document.getElementById("maindiv").innerHTML = "<div id='itembrd' onClick=\"testnodejs();\"><div id='item'>Nodetest</div></div>"; xmlHttp.open("GET", url, true) xmlHttp.onreadystatechange=function() { document.getElementById("maindiv").innerHTML += xmlHttp.status + " "; if (xmlHttp.readyState==4) { console.log(xmlHttp.responseText); document.getElementById("maindiv").innerHTML += xmlHttp.responseText + '<br />\n'; } } xmlHttp.send(null) } 

在config.xml中我有

 <access origin=".*" /> 

请求返回一个readyState 4,但是当你想要一个200的时候,它的状态也是0.任何人都有线索?

所以,我把这个放在一边了几天。 带着新鲜放弃回来,并修复它。 问题是在node.js服务器端不返回200,但是0.这是因为允许来源。 我重写了这个响应标题:

 response.writeHead(200, { 'Content-Type': 'text/plain', 'Access-Control-Allow-Origin' : '*' }); 

瞧! 探戈!

现在我有一个非常轻量级的解决scheme,用于在cordova / phonegap应用程序中进行XHR请求。