2015-11-02 85 views
0

我正在制作一个手机混合应用程序。我解析json使用xmlhttpRequest.My问题是,当我尝试在浏览器上获取响应,http.status是200和响应是好的,但是当我尝试运行Android设备上的代码,然后http.readystate出来是4但http.status来到0,我得到空的回应。我已经搜索了很多链接,并使用CORPS,但问题仍然存在。任何人都可以告诉我,我怎样才能得到http.status作为200在Android设备上?以下是我的代码。xmlhttpRequest状态在电话中返回0

function myFunction(){ 
alert("inside function"); 
var http; 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
    http = new XMLHttpRequest(); 
    alert(" inside if"); 
} 
else 
{ 
    // code for IE6, IE5 
    alert("inside else"); 
    http = new ActiveXObject("Microsoft.XMLHTTP"); 
} 
    var url = "http://localserver/abc/v1/signup"; 
    var params = "email=adsefgbbgtrhtrh &password=tyujkuykyuil &phone=95876894760458 "; 
    http.open("POST", url, true); 
    http.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
    http.onreadystatechange = function() { 
     //Call a function when the state changes. 
     if(http.readyState == 4) { 
      alert("inside if"+http.responseText); //Here i get empty text 
     } 
     alert("inside if function"+http.status+"state"+http.readyState); //Here http.status is 0 and readystate is 4 
    } 
    http.send(params); 
    response.writeHead(200, { 
     'Content-Type': 'text/html', 
     'Access-Control-Allow-Origin' : '*'}); 
    alert("ouside if"+http.status+"state"+http.readyState);} 

在服务器端我已经加入到行:

header('Access-Control-Allow-Origin:*');header('Content-Type: application/x-www-form-urlencoded'); 

我还设置了访问起源config.xml中

<access origin="*" subdomains="true"/> 
<access origin="http://localserverIP*" subdomains="true"/> 
<access origin="http://google.com" subdomains="true"/> 

请帮助!

+0

状态0在电话应用程序上可以正常工作,但如果响应为空,则说明有问题,可能与白名单插件或内容安全策略元标记有关 – jcesarmobile

回答

0

我在使用XMLHttpRequest时遇到了同样的问题。所以我使用$ .ajax,一切正常。我知道$ .ajax基于XMLHttpRequest,但它的工作原理和开发更容易。即使答案有点晚,我希望它有帮助。 =)