2012-08-29 47 views
1

我在node.js中写了一个HTTP服务器科尔多瓦/ PhoneGap的&node.js的:XMLHttpRequest的返回状态0

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 /科尔多瓦应用服务请求:

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) 
} 

在config.xml中我有

<access origin=".*" /> 

请求返回的readyState 4也是0状态时,你会期望一个200人有一个线索?

回答

3

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

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

,瞧!探戈!

现在我有一个来自cordova/phonegap应用程序的XHR请求的非常轻量级的解决方案。

相关问题