0

i've got a problem with my phonegap project, this code good work on web browser but no in android emulator . in emulator always get 'Gagal'(failed message ) ,whats wrong with this ?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Loading data into a PhoneGap app</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
</head>
<body>
    <ul id="your-tweets"></ul>

    <script>
$(document).ready(function(){
    $.ajax({
        url: 'http://127.0.0.1/json.php',
        dataType: 'jsonp',
        jsonp: 'jsoncallback',
        timeout: 5000,
        success: function(data, status){
            $('#your-tweets').append('<li>Sukses</li>');
        },
        error: function(){
            $('#your-tweets').append('<li>Gagal</li>');
        }
    });
});
</script>


</body>
</html>
  • Why r u trying to use jsonp ithink json is works fine for you & define callback in url also – Jain Feb 27 '14 at 08:44
  • I think you made mistake in url and dataType part coz you trying to access your localhost php file in your emulator better put your file in server side and try to access it anyway you gonna to that finally ...and plz study the difference between JSON and JSONP and where it is used! ... – kamesh Feb 27 '14 at 08:59

3 Answers3

1

The url in your ajax post is wrong. You are referring to 127.0.0.1 which is localhost on the phone itself. Possible duplicate of this other stackoverflow question.

Your answer is here

Community
  • 1
  • 1
0

First of all, you are attempting to use JSONP when you don't need to. JSONP is a completely different method of communication to JSON. JSONP will return a call to a function you specify (in this case jsoncallback) when the request is completed, where as JSON will simply return a string of data which can be parsed. JSONP works by adding a <script> field to the body of your content, where as JSON works by using the browsers AJAX request functionality.

You are also attempting to access a web server on the local host. This will of course work if you visit the page on a browser on the same machine as the server you're trying to access, but on the emulator you'll need to access the server by its specific IP on the network.

Try getting the IP of the server by using either

ipconfig

..if it's Windows or

ifconfig

..if it's Linux

Use the IP address specified for either the Ethernet adapter or WiFi adapter, depending on which one you are currently using.

Whether or not this will work, I'm not sure, as I believe Android Emulators do not actually connect to your network, but go through some sort of NAT or Bridging to your current network connection.

If you had your service hosted on an actual INTERNET IP address, then you could use that.

Note: in order to use AJAX on a host other than the one you're visiting, you may need to provide a WhiteList in the res/xml/cordova.xml file, as described here

Seidr
  • 4,946
  • 3
  • 27
  • 39
  • Exactly, 127.0.0.1 is your localhost address. In the Android Emulator, that would translate to the Android devices localhost, and as such the Android copy would attempt to connect to a web server on the Android device, not on your Windows/Linux machine. – Seidr Feb 27 '14 at 08:41
0

127.0.0.1 always points to your phone itself and am sure you are not running your server on your phone. so please change the ipaddress to the system where you are running server. it will work for sure.

Rahul
  • 710
  • 1
  • 8
  • 25