PDA

View Full Version : ST2 Ajax Error on Androd 2.2+



trinitrotoluen
27 Apr 2012, 8:56 PM
Hi,

My App running well on desktop browser without error. But when I tested on browser android 2.2+ I got an error :

Uncaught Error: You're trying to decode an invalid JSON String

I'm using simple ajax request something like this :


Ext.Ajax.request({
url: myurl,
success:function(response, opts){
var obj = Ext.decode(response.responseText);
console.log(response.responseText);
console.log(obj);
},
failure :function(response, opts){
}
});

FYI. - I tested my json response on jsonlint.com and valid.
- running well on my desktop browser (chrome, safari)
- I tested on my iphone iOS 5.1 running well no error

any clue what's the problem :-/ ?

thx

mitchellsimoens
30 Apr 2012, 6:59 AM
What headers are in the response? Is the type valid for json?

trinitrotoluen
30 Apr 2012, 7:54 AM
What headers are in the response? Is the type valid for json?

mitchellsimoens, I already replied your question about this problem here, please check it out :)

http://www.sencha.com/forum/showthread.php?199118-ST2-Ajax-Error-on-Androd-2.2

thanks

CarlosMS
30 Apr 2012, 9:37 AM
trinitrotoluen,

Run your app. again in Chrome and verify whether you see the error in Chrome's JavaScript console. Then, do the same in Safari and see whether you get the error logged in its console too. In my case Chrome logs the error but the app. continues running normally; and, Safari doesn't log the error and the app. runs normally too. It's only when ran under an Android 2.2 or 2.3 device that the error is logged and the app. execution is halted. You can see the logs too using the Android SDK emulator + Eclipse.

I tracked this down to the CORS OPTIONS preflight call each browser makes. In my case what I found is that the REST service's response data returned for the preflight call is being concatenated to the post-preflight call's response data (i.e. the actual call that follows a successful preflight). The concatenated JSON data is then parsed by the Sencha library, which then raises the error as it should. I say "as it should" because the concatenated result of both calls is not a valid JSON structure.

I left my research here, which means I do not know if this is a Sencha Touch behavior based on different execution paths with doing preflight/post-preflight calls for each Android, iOS, Chrome, or Safari. I took the easy way out and temporarily hacked the REST service to not return a payload (just headers) on a preflight OPTIONS call. This "solved" the problem with the concatenation/parsing, but does not answer the question as to who is doing the concatenation (i.e. Sencha Touch code or the browsers) and why.

If you or Mitchell can shed any more details on this it would be greatly appreciated.

trinitrotoluen
30 Apr 2012, 10:23 AM
trinitrotoluen,

Run your app. again in Chrome and verify whether you see the error in Chrome's JavaScript console. Then, do the same in Safari and see whether you get the error logged in its console too. In my case Chrome logs the error but the app. continues running normally; and, Safari doesn't log the error and the app. runs normally too. It's only when ran under an Android 2.2 or 2.3 device that the error is logged and the app. execution is halted. You can see the logs too using the Android SDK emulator + Eclipse.

I tracked this down to the CORS OPTIONS preflight call each browser makes. In my case what I found is that the REST service's response data returned for the preflight call is being concatenated to the post-preflight call's response data (i.e. the actual call that follows a successful preflight). The concatenated JSON data is then parsed by the Sencha library, which then raises the error as it should. I say "as it should" because the concatenated result of both calls is not a valid JSON structure.

I left my research here, which means I do not know if this is a Sencha Touch behavior based on different execution paths with doing preflight/post-preflight calls for each Android, iOS, Chrome, or Safari. I took the easy way out and temporarily hacked the REST service to not return a payload (just headers) on a preflight OPTIONS call. This "solved" the problem with the concatenation/parsing, but does not answer the question as to who is doing the concatenation (i.e. Sencha Touch code or the browsers) and why.

If you or Mitchell can shed any more details on this it would be greatly appreciated.

Thanks for your reply Carlo,

Here is sample json url I try to access :
https://chrome.airasia.com/testapi/countries

After a few testing, apparently my ajax request to this url also failed on iOS 4.3.5 browser.

While browsing I read this thread :
http://stackoverflow.com/questions/6620191/does-phonegap-for-android-supports-https-ajax-requests

I'm suspecting the problem is related with https ajax request not supported in the browser ? any experience how to manage this problem ?

thanks

CarlosMS
1 May 2012, 7:36 AM
trinitrotoluene, no, I don't have experience with HTTPS AJAX calls. Is there a way to test the same call from your app. via plain HTTP and see whether it works? Also, the couple of issues I've had with invalid JSON string errors (all happened under Android and never under iOS) the LogCat messages (Eclipse->DDMS view) have been invaluable pinpointing the error when running the app. under the Android emulator.