Ajax request not working in cordova / phonegap application on real device

I am creating an Android Cordova 4.0 jQuery Mobile 1.4.2 Android application and am having problems with a specific AJAX call. I searched for similar questions and already implemented solutions there without 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 application in my browser, this request works fine. However, when I create and debug the application from a real device , the request does not give this error: {"readyState":0, "responseText":"", "status":0, "statusText":"error"}

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

Can someone help me figure out what the problem is?

+5
source share
4 answers

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

I realized that the problem is not in the code, because I noticed that the other AJAX calls that I had in the application (and were used to work fine) also fail.

So I don’t know if this is some kind of Cordova 4.0 error or something else, but at some point AJAX stopped working. I am posting this in case anyone comes across the same question.

+5
source

In my case, I upgraded from Cordova 3.7 to Cordova 5. Here's what solved it for me: add the cordova-plugin-whitelist plugin

+4
source

Due to the new content security policy for Android requests, Ajax is blocked.

try running and see if this works.

Open the config.xml file

Replace

 <access origin="*" /> 

FROM

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

prepare the phonegap project and rebuild it and check the real device.

Regards, Jagat

+2
source

Turns out I just miss the following plugin:

Cordova-plugin-whitelist

After I installed it, remove the Android platform, add the Android platform, create and run, it will work!

0
source

Source: https://habr.com/ru/post/1207258/


All Articles