Ajax Requests Fail in Android 4.x

8,082 views
Skip to first unread message

Tom

unread,
Apr 9, 2012, 1:22:19 PM4/9/12
to phon...@googlegroups.com
I have a simple app that makes some ajax calls, get requests pulling in some XML content. Works perfectly in all browsers, on iOS, Android 2.1 and 2.3. It even works in the stock Android browser of Android 4.x and Chrome Beta for Android.

It will not work (no apparent errors, simply loads nothing) in Android 4.0 and 4.0.3. Is this a known bug?

Captain Obvious

unread,
Apr 9, 2012, 2:52:00 PM4/9/12
to phon...@googlegroups.com
On mobile, using "file" protocol, be sure to also check for xhr status=0 :

https://groups.google.com/forum/#!msg/phonegap/1e0z4I9Ps68/pfxXSbzWIoMJ


Ladar Levison

unread,
Apr 9, 2012, 3:54:07 PM4/9/12
to phon...@googlegroups.com, Tom
> --

Set UsePolling = true (line 605 for me) in the Cordova Javascript file!
It took me a week to solve this bug!


Tom

unread,
Apr 11, 2012, 10:10:10 AM4/11/12
to phon...@googlegroups.com, Tom
UsePolling sadly did not work.

The XHR status suggestion shouldn't apply (I think), I'm using jQuery 1.7.1 and my ajax calls are going through no problem in Android 2.1 and 2.3. I might also add that the content I'm bringing in is all 100% local, I'm not connecting to any outside server, just some .xml files that are packaged with the app.

Simon MacDonald

unread,
Apr 11, 2012, 10:28:32 AM4/11/12
to phon...@googlegroups.com
Try taking jQuery out of the equation and doing a XHR directly to the file to see if it works. Here is an example where I load a .sql file from the assets folder.


Simon Mac Donald
http://hi.im/simonmacdonald


--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com

Tom

unread,
Apr 11, 2012, 11:12:45 AM4/11/12
to phon...@googlegroups.com
Might there be a jQuery solution? I'll rewrite my calls in standard JS as a last ditch resort, but that would be an inelegant solution given the nature of my app. I would think there must be something I can do with jqXHR.status.

I'm just trying to avoid rewriting a good bit of code when the issue is isolated to the web view of a single version of one OS.

Simon MacDonald

unread,
Apr 11, 2012, 11:22:06 AM4/11/12
to phon...@googlegroups.com
All I'm saying is to try to load the file using plain vanilla XHR first. If that works we've narrowed it down to a jQuery issue and we can go from there.
On Wed, Apr 11, 2012 at 11:12 AM, Tom <tderk...@gmail.com> wrote:
Might there be a jQuery solution? I'll rewrite my calls in standard JS as a last ditch resort, but that would be an inelegant solution given the nature of my app. I would think there must be something I can do with jqXHR.status.

I'm just trying to avoid rewriting a good bit of code when the issue is isolated to the web view of a single version of one OS.

--

Tom

unread,
Apr 11, 2012, 11:36:23 AM4/11/12
to phon...@googlegroups.com
Embarrassing moment: just realized Ajax partially works for me.

My app is such that I have an "index" xml file that is loaded upon initial page load. This index is used to power an autocomplete search box. If an option is selected from a search box, that triggers a separate ajax call to an XML file containing information on that option.

My index file loads, individual selections do not. It's still a mystery to me why the latter ajax calls fail in Android 4.0 web view exclusively, in fact it might be more of a mystery now.

Tony Ivanov

unread,
Apr 27, 2012, 10:50:47 AM4/27/12
to phon...@googlegroups.com
I'm having the same problems, all jQuery XHR calls simply fail on Android 4.x without a trace. 
The application works as fine on Android 2.1 and iOS but not Android 4.x
I haven't had the luck to notice a single xhr passing yet.
I'm gonna try as simon suggested and if that is the case then it should be possible to monkeypatch the jQuery method (no need to rewrite everything Tom :) )

On Wednesday, 11 April 2012 17:22:06 UTC+2, Simon wrote:
All I'm saying is to try to load the file using plain vanilla XHR first. If that works we've narrowed it down to a jQuery issue and we can go from there.

Simon Mac Donald
http://hi.im/simonmacdonald


On Wed, Apr 11, 2012 at 11:12 AM, Tom <tderk...@gmail.com> wrote:
Might there be a jQuery solution? I'll rewrite my calls in standard JS as a last ditch resort, but that would be an inelegant solution given the nature of my app. I would think there must be something I can do with jqXHR.status.

I'm just trying to avoid rewriting a good bit of code when the issue is isolated to the web view of a single version of one OS.

--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to

Simon MacDonald

unread,
Jun 12, 2012, 1:23:25 PM6/12/12
to phon...@googlegroups.com
This points to a bug in jQuery. You should open an issue on their form. If plain vanilla XHR works then it is not a PhoneGap problem. Try this code for instance:

    var request = new XMLHttpRequest();

        request.open("GET", "http://search.twitter.com/search.json?q=phonegap", true);

        request.onreadystatechange = function(){//Call a function when the state changes.

            console.log("state = " + request.readyState);

            if (request.readyState == 4) {

                if (request.status == 200 || request.status == 0) {

                    console.log("*" + request.responseText + "*");

                }

            }

        }

        request.send();

On Tue, Jun 12, 2012 at 8:32 AM, Philip K <p...@symvaro.com> wrote:
Same problem here. Works fine with iOS and Android < 4.0.3, but with Android 4.0.3 just the "pure" XMLhttprequest with the handling for the status code 0 works. Has anyone already fixed the jQuery or Phonegap source files for this problem? 

Thanks

Philip K

unread,
Jun 13, 2012, 9:35:16 AM6/13/12
to phon...@googlegroups.com
Yeah plain vanilla XHR works fine, but are you sure that this is a jQuery bug? I'm asking because the application throws the following exception just with Android 4.x, in Android 2 there is no such exception and everything works fine

06-13 15:14:19.257: W/webcore(563): java.lang.Throwable: EventHub.removeMessages(int what = 107) is not supported before the WebViewCore is set up.
06-13 15:14:19.257: W/webcore(563): at android.webkit.WebViewCore$EventHub.removeMessages(WebViewCore.java:1683)
06-13 15:14:19.257: W/webcore(563): at android.webkit.WebViewCore$EventHub.access$7900(WebViewCore.java:926)
06-13 15:14:19.257: W/webcore(563): at android.webkit.WebViewCore.removeMessages(WebViewCore.java:1795)
06-13 15:14:19.257: W/webcore(563): at android.webkit.WebView.sendOurVisibleRect(WebView.java:2917)
06-13 15:14:19.257: W/webcore(563): at android.webkit.ZoomManager.setZoomScale(ZoomManager.java:593)
06-13 15:14:19.257: W/webcore(563): at android.webkit.ZoomManager.access$1700(ZoomManager.java:49)
06-13 15:14:19.257: W/webcore(563): at android.webkit.ZoomManager$PostScale.run(ZoomManager.java:984)
06-13 15:14:19.257: W/webcore(563): at android.os.Handler.handleCallback(Handler.java:605)
06-13 15:14:19.257: W/webcore(563): at android.os.Handler.dispatchMessage(Handler.java:92)
06-13 15:14:19.257: W/webcore(563): at android.os.Looper.loop(Looper.java:137)
06-13 15:14:19.257: W/webcore(563): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-13 15:14:19.257: W/webcore(563): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 15:14:19.257: W/webcore(563): at java.lang.reflect.Method.invoke(Method.java:511)
06-13 15:14:19.257: W/webcore(563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-13 15:14:19.257: W/webcore(563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-13 15:14:19.257: W/webcore(563): at dalvik.system.NativeStart.main(Native Method)

Simon MacDonald

unread,
Jun 13, 2012, 10:14:18 AM6/13/12
to phon...@googlegroups.com
Yeah, that is not a PhoneGap issue. You can go star this issue:


which describes exactly what you see below and PhoneGap is not involved.

MajiD

unread,
Feb 19, 2013, 3:48:00 PM2/19/13
to phon...@googlegroups.com
Thanks guys,

Plain XMLHttpRequest fixed the issue for me on Android 4.1.1

TheNuvis

unread,
Apr 23, 2014, 12:03:00 PM4/23/14
to phon...@googlegroups.com

i am working with phonegap in android v4.. in v2.2 works ok but in v4 no...
your solution about vanilla ..example simple..?
any idea?

____________________________________________

Thanks
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com

--
You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com

Louis R

unread,
Jun 19, 2014, 5:05:26 PM6/19/14
to phon...@googlegroups.com
I have the same problem, any fix ?

thanks!

Louis R

unread,
Jun 20, 2014, 7:43:50 AM6/20/14
to phon...@googlegroups.com
The XHR fix works fine : Thanks Simon for your blog post
Reply all
Reply to author
Forward
0 new messages