xmlhttpRequest状态在phonegap中返回0 [英] xmlhttpRequest status returns 0 in phonegap
问题描述
我在phonegap制作一个混合应用程序。我使用xmlhttpRequest.My解析json问题是,当我尝试获取浏览器上的响应,http.status是200和响应是确定,但是当我尝试运行Android设备上的代码,然后http.readystate出来是4但http.status变为0,我得到空响应。我搜索了许多链接,并使用CORPS也,但问题仍然存在。任何一个可以告诉我,我怎么能得到http.status作为200在Android设备?下面是我的代码。
I am making a hybrid app in phonegap. I am parsing json using xmlhttpRequest.My Problem is that when i try to fetch the response on browser,http.status is 200 and response is ok but when i try to run the code on android device then http.readystate comes out to be 4 but http.status comes 0 and i get empty response .I have searched many links and used CORPS also but the problem still persist. Can any one please tell me that how can i get the http.status as 200 on android device? Below is my code.
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);}
在服务器端我已经添加到行:
On server side i have added to lines:
header('Access-Control-Allow-Origin:*');header('Content-Type: application/x-www-form-urlencoded');
我还在config.xml中设置了访问源。
I have also set the access origin in config.xml
<access origin="*" subdomains="true"/>
<access origin="http://localserverIP*" subdomains="true"/>
<access origin="http://google.com" subdomains="true"/>
请帮助!
推荐答案
当我使用XMLHttpRequest时,我有同样的问题。所以我使用$ .ajax和一切工作。我知道$ .ajax是基于XMLHttpRequest,但它的工作和开发更容易。我希望它帮助,即使答案是有点晚了。 =)
I had the same problem when I used XMLHttpRequest. So I used $.ajax and everything was working. I know that $.ajax is based on XMLHttpRequest, but it works and the development is more easy. I hope it helps even if the answer is a bit late. =)
这篇关于xmlhttpRequest状态在phonegap中返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!