Ajax call on cordova not sending request when using mobile network

224 views Asked by At

i am developing android apps on cordova . I have a jquery ajax call to my server that works on my browser, it works on my device when I have it connected to my local proxy for http sniffing and local wi-fi , but just hangs when it's off my proxy on the wifi or on the cell network .

ajax code:

        $.ajax({
            url: 'http://192.xxx.x.xx/xxxApi/api/controller/GET?jsonData='
                 + JSON.stringify({
                     'SecurityCode': z.toUpperCase(),
                     'LoginID': x,
                     'password': y
                 }),
            type: 'GET',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            cache: false,
            success: function (data) {
                var userName = data.UserName;

config.xml

<content src="index.html" />
<access origin="*" />
<plugin name="cordova-plugin-whitelist" version="1" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />

Is there something I'm missing? What am I doing wrong?

1

There are 1 answers

0
BSB On

Ajax does not allow external IP / Domain call because of "Same origin policy", but you can still use the AJAX function to call external domain / IP :

You need to trigger JSONP behavior with $.getJSON() by adding &callback=? on the querystring, like this:

$.getJSON("http://EXTERNAL_DOMAIN_OR_IP_LOCATION/?param=value&callback=?",
function(data) {
    doSomethingWith(data); 
}); 

Without using JSONP you're hitting the same-origin policy which is blocking the XmlHttpRequest from getting any data back.