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

Cordova/phonegap&;node.js:XMLHttpRequest返回状态0,node.js,cordova,xmlhttprequest,Node.js,Cordova,Xmlhttprequest,我已经在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); }).lis

我已经在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/i.t?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)
}
函数testnodejs(){
xmlHttp=GetXmlHttpObject()
if(xmlHttp==null){
警报(“您的webbrowser不支持HTTP请求”);
返回;
}
变量url=”http://domain.com:8124/i.t?hi=hi!";
console.log(url);
document.getElementById(“maindiv”).innerHTML=“Nodetest”;
open(“GET”,url,true)
xmlHttp.onreadystatechange=函数(){
document.getElementById(“maindiv”).innerHTML+=xmlHttp.status+“”;
if(xmlHttp.readyState==4){
log(xmlHttp.responseText);
document.getElementById(“maindiv”).innerHTML+=xmlHttp.responseText+'
\n'; } } xmlHttp.send(空) }
在config.xml中,我有

<access origin=".*" />


该请求返回readyState 4,但在您预期的状态为200时也返回状态为0。有人有线索吗?

所以,我把这个放在一边几天。带着新的放纵回到家里,把它修好了。问题出现在node.js服务器端,它不返回200,而是返回0。那是因为允许起源。我重写了响应标题,因此:

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

瞧!探戈

现在我有了一个非常轻量级的解决方案,用于cordova/phonegap应用程序中的XHR请求