最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery's ajax function does not work in Android PhoneGap application - Stack Overflow

programmeradmin1浏览0评论

I want to access open API through ajax in mobile application, its work fine in iphone but does not working in Android phonegap application:

returning error->"error message-null,typeerror-Result of expression 'data'[null] is not an object and error status-parsererror"

Is there any browser setting need to follow.

I want to call the web service in android-phonegap application:

$.ajax({  
   url:'stringURL',  
   beforeSend: function(x) {      
     x.setRequestHeader('Authorization','username/pwd');  
   },  
   dataType:"xml",  
   contentType:'application/xml',  
   timeout:10000,  
   type:'POST',  
   success:function(data) {  
     alert(data);  
   },  
   error:function(XMLHttpRequest,textStatus, errorThrown) {     
     alert("Error status :"+textStatus);  
     alert("Error type :"+errorThrown);  
     alert("Error message :"+XMLHttpRequest.responseXML);  
   }
});

I want to access open API through ajax in mobile application, its work fine in iphone but does not working in Android phonegap application:

returning error->"error message-null,typeerror-Result of expression 'data'[null] is not an object and error status-parsererror"

Is there any browser setting need to follow.

I want to call the web service in android-phonegap application:

$.ajax({  
   url:'stringURL',  
   beforeSend: function(x) {      
     x.setRequestHeader('Authorization','username/pwd');  
   },  
   dataType:"xml",  
   contentType:'application/xml',  
   timeout:10000,  
   type:'POST',  
   success:function(data) {  
     alert(data);  
   },  
   error:function(XMLHttpRequest,textStatus, errorThrown) {     
     alert("Error status :"+textStatus);  
     alert("Error type :"+errorThrown);  
     alert("Error message :"+XMLHttpRequest.responseXML);  
   }
});
Share Improve this question edited Dec 4, 2012 at 8:26 Mayur Birari asked Sep 14, 2010 at 11:07 Mayur BirariMayur Birari 5,8358 gold badges36 silver badges61 bronze badges 16
  • 1 Which browser and what does your code look like? – Nick Craver Commented Sep 14, 2010 at 11:12
  • 2 @Mayur - Thanks for updating your code, as for the browser: you shouldn't assume the defaults, or force others to...everyone I know with an android phone uses a non-default browser. – Nick Craver Commented Sep 14, 2010 at 11:51
  • 3 Could it be that the request is cross domain and jQuery ajax fails? I usually use JSON with a callback function to deal with that. I only did that once with PhoneGap and it worked. – Matthew Rygiel Commented Mar 30, 2011 at 15:56
  • 2 That's because you're not giving us the information we need to help you. Try updating to the latest version of jQuery and PhoneGap, if the problem still exists, post a link to a live version your app so there is something to test on. – Sindre Sorhus Commented Apr 9, 2011 at 17:18
  • 3 @Mayur, don't forget that you can add a bounty to your question to motivate people to give good answers. You should probably edit your question to specify which browsers on which platforms fail, and address @Matthew's concern about cross domain requests before posting the bounty, save everyone the time to read all these ments. – sarnold Commented Apr 29, 2011 at 6:02
 |  Show 11 more ments

1 Answer 1

Reset to default 3 +50

It seems that there is a parse error on the data fetched. You want the XMLHttpRequest to parse XML data, so the fetched URL has to return valid XML. There are several possible reasons why that works on one but fails on another platform:

-The browser request headers may be different, resulting in a different server answer. Some servers may give HTTP 200 status answers with some error message in case of problems, which doesn't parse to valid XML.

-The returned XML may be problematic and parses well on the iPhone due to quirks in the browser there but fail on another WebKit version / variant the Android uses.

-The fetched data is damaged due to other request headers, provider, proxy or other effects. For example some proxys only handle HTTP/1.0 requests, and bad behaving servers serve HTTP/1.1 with chunked encoding every time, so the XML will be damaged by the chunk headers.

For debugging purposes you can change the request to a text request and show the fetched data, or even save it to the devices SD card if possible in your application. You can then verify it has arrived intact and is in fact valid XML.

发布评论

评论列表(0)

  1. 暂无评论