Kirankumar Dafda
Kirankumar Dafda

Reputation: 2384

Ajax json get data from domain

I am working on phonegap and trying to get list of user using ajax call but my function is returning nothing on mobile its working fine in browser

here is the function and url data is in xml format

$(document).ready(function(){
$.ajax({
    type: "POST",
    url: "http://my_server_url/user/selectuser",
    dataType: 'jsonp',
    crossDomain: true,
    processData: true,
    cache: false,
    success: function(data)
    {
        alert("Success:"+data.detail[0].Username);
    },
    error: function(data){
        alert("Error:"+data);
    },
    statusCode: {
        404: function() {
            alert( "page not found" );
        }
    }
}); 
});

Thanks

Upvotes: 2

Views: 110

Answers (2)

Krunal
Krunal

Reputation: 327

With Phonegap version "Cli-5.2.0", whitelist plugin need to be use to successfully make ajax call on Android. Add below mentioned line in config.xml to use whitelist plugin with Phonegap cloud build.

<gap:plugin name="cordova-plugin-whitelist" source="npm" />
<access origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />

Check https://github.com/apache/cordova-plugin-whitelist for whitelist plugin description

Upvotes: 0

Andrew
Andrew

Reputation: 14526

The general steps I go through for checking problems like this on mobile are:

  1. The simplest thing you can do is try pulling up the resource via a regular browser window on your phone and see what's different. Just type in the URL and see what happens. If you can't pull it down on a generic Chrome / Mobile Safari tab, the error may help you figure out what the problem is. If you can pull it down, the problem likely resides in your code (a typo in the URL perhaps). This won't work if you aren't doing a GET request, but it may at least help you figure out if your phone can reach your server.
  2. Connect your device to your PC on USB and observe the request in a Safari/Chrome debugger's Network tab. You should probably be doing this all the time anyway, but it always seems to be my step #2.
  3. Set one breakpoint before your request is sent, another in the success handler and a third in the error handler. The request may be successful, but an error immediately after the response could be stopping your program. Or maybe the response text is not what you were expecting (a web page rather than a JSON stream.)
  4. If you are doing a non-GET request (like you are here), you can try running your own AJAX request directly in the debugger console. If you duplicate the request exactly, it shouldn't give you any more information than watching the regular request in the network tab, but it does allow you to modify the request multiple times without recompiling your app. It can be a huge time-saver.

If your phone can't reach the resource at all (#1) then maybe you have a specialized local environment that your phone doesn't share. For example, you might be calling localhost or 127.0.0.1, which your phone will never reach (because these IP addresses have a special meaning on most networks and basically means 'me' or 'this computer'). Or maybe you're calling out to something like 192.168.xxx.xxx or 10.xxx.xxx.xxx, which your phone can't reach because it isn't on that private network (e.g if you are not on the same wifi). In this case, you have two options:

  1. figure out how to expose those resources to outside requests, or
  2. get your phone into your network environment. In extreme cases, I've actually had to build my own DNS server, change my phone's networks settings to use that, and redirect requests going to specific domain names.

Upvotes: 1

Related Questions