Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery's ajax function does not work in Android PhoneGap application

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);  
   }
});
like image 737
Mayur Birari Avatar asked Sep 14 '10 11:09

Mayur Birari


1 Answers

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.

like image 65
dronus Avatar answered Oct 30 '22 17:10

dronus