6

I'm building a Cordova 4.0 jQuery Mobile 1.4.2 Android app and I'm having issues with a particular AJAX call. I've looked for similar questions and already implemented the solutions there with no success.

Here's what happens:

I have the following AJAX call:

var request = $.ajax({
        type: "GET" ,
        crossDomain: true,
        url: 'http://pubads.g.doubleclick.net/gampad/adx?iu=/XXX/YYY&sz=300x50&c=123456789'
    });

    request.done(function (response, textStatus, jqXHR){

        console.log(response);
    });

    request.fail(function (jqXHR, textStatus, errorThrown){
        console.error("DFP Plugin Error: " + textStatus, errorThrown);
    });

When I run my app in my computer's browser, that request works perfectly. However, when I build and debug the app from a real device, the request fails giving this error: {"readyState":0, "responseText":"", "status":0, "statusText":"error"}

I've already enabled $.support.cors = true; and $.mobile.allowCrossDomainPages = true;, and I already have <access origin="*" />) in my config.xml file.

Could someone help me figure out what the problem is?

Albert
  • 1,516
  • 3
  • 24
  • 55

4 Answers4

6

Well, this is not the kind of answer I was expecting, but this what I did to solve this: create a completely new Cordova 4.0 project and copy there the www folder from the other one. Then build and run in eclipse as usual and everything worked without changing one single line of code anywhere in the project.

I realized the problem wasn't in the code because I noticed that other AJAX calls that I had in the app (and used to work fine) were also failing.

So I don't know if this is some Cordova 4.0 bug or something, but at some point AJAX stopped working. I'm posting this in case someone runs into the same issue.

Albert
  • 1,516
  • 3
  • 24
  • 55
  • same issue here... sick. :/ – aZtraL-EnForceR Jan 18 '15 at 07:48
  • It happened to me too. But after googling "cordova ajax call not work" I uninstalled my app from the device and emulator, wipe emulator, delete emulator and create a new one, and oh, of course, fiddle with my dns server settings, since my call from dnsmasq was kinda funny. – Rafael Mena Barreto Sep 01 '15 at 03:25
4

In my case I was upgrading from cordova 3.7 to cordova 5 Here is what solved it for me: add the plugin cordova-plugin-whitelist

standup75
  • 4,734
  • 6
  • 28
  • 49
  • 1
    Link to docs: https://cordova.apache.org/docs/en/5.1.1/guide_appdev_whitelist_index.md.html and the command: `cordova plugin add cordova-plugin-whitelist` – Yaroslav Stavnichiy Aug 12 '15 at 23:44
3

Because of New Content Security Policy for android Ajax Requests are blocked.

try following and see if that works.

Open your config.xml

Replace

<access origin="*" />

With

<access origin="http://*" />
<access origin="https://*" />

prepare the phonegap project and build it again and check on real device.

Regards, Jagat

-1

Turns out I was just missing the following plugin:

cordova-plugin-whitelist

After I installed it, remove the android platform, re-added the android platform, build and run, it worked!

Sỹ Phạm
  • 530
  • 6
  • 16