2015-11-02 82 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,但它的工作原理和開發更容易。即使答案有點晚,我希望它有幫助。 =)