1

Here is my web service that works perfectly fine in Fiddler and in my browser: http://localhost:11458/PlayerSvc.svc/GetPLayers

I then tried to call this via Jquery in phonegap and it failed: here is my jquery in phonegap using Eclipse:

<script type="text/javascript">
$.ajax({
        url: 'http://localhost:11458/PlayerSvc.svc/GetPlayers',
            dataType: 'json',
           // timeout: 55000,
           // beforeSend: function(xhr) {
                //Possible to set any required headers here
                //xhr.setRequestHeader('Authorization', 'username password');
           // },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('failed');
            },
            success: function(data) {
                alert('success');
            }
        });
</script>

To make sure there was nothing wrong with my jquery I tested another webservice with this code and it was successful. I am not sure how to gather more information on how to debug this. I tried wrapping my javascript with console.log(javascript above) and I could not get that to work. As you can see above I just used the alert function to output the failed error message. Any ideas? I think i may try using a try catch and then seeing if I can output the exception...

LiquidChild
  • 37
  • 1
  • 1
  • 8
  • Are you testing in an emulator or a real device? for emulator see Kyberias answer. Otherwise use the network IP of your development machine on your network, probably in the 192.168.*.* range. – longhairedsi Jan 21 '12 at 12:43

2 Answers2

3

Are you running the PhoneGap application in the Android emulator? If yes, I think the localhost (127.0.0.1) in the emulator refers to the emulator itself and not the host machine. Perhaps you need to try another IP address (possibly 10.0.2.2 works).

Here's a question/answer that addresses this issue:

test the localhost in android emulator

Community
  • 1
  • 1
Kyberias
  • 1,263
  • 1
  • 14
  • 23
  • thanks you Very Much! that was the issue. I changed local host to 10.0.2.2 So http://10.0.2.2:11458/PlayerSvc.svc/GetPLayers and it worked. Thanks again – LiquidChild Jan 21 '12 at 15:01
0

You need to allow whitelist urls and add your host address to external hosts in your phonegap.plist file.

http://geekaboo.wordpress.com/2011/11/20/ios5-whitelist-url-unauthorized-using-phonegap/

gracezlive
  • 53
  • 6
  • Well I am using Android and instead of the phonegap.plist file there is a phonegap.xml and i have added http://localhost:11458 to that file, so I don't think that is the problem.. Also strangely the test webservice that I was using the ensure that my javascript is not the issue worked even though i had not added it to the phonegap.xml file.... – LiquidChild Jan 21 '12 at 05:13