Kaiesh
Kaiesh

Reputation: 1052

PhoneGap not issuing AJAX (jsonp) request

I've been trying to battle this issue all day, and have googled all over the place. It may be that I just can't find that one page with the solution... but I have looked at problems and solutions such as:

Ajax call on phonegap not sending request

JQuery mobile + Phonegap : Ajax calls not working on Android Emulator

and neither of the solutions there appeared to have resolved my issue.

All I want to do is issue an AJAX call to my remote server, upload a few parameters and download some content. This script works perfectly in a browser, but depending on minor changes to the file in Phonegap, either throws back a "timeout" error, or simply just says "error". The code is as follows (the AJAX endpoint is a dev endpoint, and I'll leave it up so you can try hitting it as well):

<html>
<head>
<script type="text/javascript" charset="utf-8" src="jquery-1.10.2.min.js"></script>
</head>

<body>
<ul>
<div id="app-status-ul">
<li>Loaded</li>
</div>
</ul>
</body>
<script>
var serverRegHit = function(data, httpStat){
    $("#app-status-ul").append('<li>Server Reg OK, HTTP status: '+httpStat+'</li>');
    $("#app-status-ul").append('<li>Data Status: '+JSON.stringify(data)+'</li>');
};
var serverRegFail = function(data, httpStat){
    $("#app-status-ul").append('<li>Server Reg FAIL, HTTP status: '+httpStat+'</li>');
    $("#app-status-ul").append('<li>Data Status: '+JSON.stringify(data)+'</li>');
};
      $.ajax({url:"http://keb.bz/gcm/?deviceid=12345",
              jsonp: 'callback',
              dataType: 'jsonp',
              timeout: 25000,
              success: serverRegHit,
              error: serverRegFail});

</script>
</html>

I have tried using cordova 2.9, and downgraded to 2.5, but this does not seem to have helped either.

My config.xml has whitelisted .* and the keb.bz domain, the permissions give the app access to the internet in the manifest - and I assume this works because it is able to successfully retrieve a GCM Device ID.

I have tried using JSON, JSONp, TEXT, and various dataType's through AJAX, but none of these have made any difference.

Is there another configuration point in Cordova/PhoneGap that I am missing?

Any help here would be appreciated.

Upvotes: 1

Views: 4172

Answers (2)

Shaakir
Shaakir

Reputation: 484

I had a similar error today when i updated my app using online PhoneGap compiler...

i last complied the app in Apr 2015. Then did a few tweeks today , nothing major, complied it, and all my ajax request fell over. I did and alert of error strings and all it said "timeout".

i then specified the phonegap-version to 3.7.0, and re-compiled... all my ajax working again. Not sure what CLI5.1 and 5.2 changed but it killed my app.

Short term solution, as soon 3.7 probably won't be supported anymore, and then i will be stuck for changes... good time to go native i guess

Upvotes: 1

Kaiesh
Kaiesh

Reputation: 1052

Thanks to @xioawl for making me double check my manifest.xml. Turns out I had not correctly configured permissions there.

In the end, I wound up using Cordova 2.5, jQuery 1.10, (did not use jQuery Mobile), setting my res/config.xml with the following access paths:

<access origin="http://127.0.0.1*" />
<access origin="http://keb.bz" subdomains="true" />
<access origin=".*" />  <!-- only during development -->

Placing this snippet after including all JavaScript libraries:

<script>
$(document).bind("mobileinit", function(){
   $.mobile.allowCrossDomainPages = true;
});
</script>

And using the following manifest.xml permissions:

<uses-permission android:name="android.permissions.INTERNET" />
<uses-permission android:name="android.permissions.NETWORK_ACCESS" />
<uses-permission android:name="android.permissions.ACCESS_NETWORK_STATE" />

My JSONp Query continues to look as follows:

$.ajax({url:"http://keb.bz/gcm/?deviceid=12345",
        jsonp: 'callback',
        dataType: 'jsonp',
        timeout: 25000,
        success: serverRegHit,
        error: serverRegFail});

Hope this is useful to someone!

Upvotes: 3

Related Questions