0

I'm very new to phonegap. I installed the phonegap2.1.0 in xcode4.5. Now phonegap is working fine. But how can I call a webservice and get a json response for phonegap? My code is below, but it isn't working. Can anybody help me?

$(document).ready(function () {
    $.ajax({
    alert("alert");
    url: "http://localhost:55022/WebSite1/sample.asmx/returnEmployees",
    data: null,
    crossDomain: true,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg, textStatus, jqXHR){
            alert(textStatus);
            //var theRealData = msg.d;
           /*
           $.each(theRealData.employees, function (index, item) {
           // at this point, item is an array, since theRealData.employees is an array of arrays
           var col1 = item[0];
           var col2 = item[1];
           alert(col1);
           }); */
    },error:function(jqXHR, textStatus, errorThrown){
    console.log(textStatus +"------" +errorThrown + jqXHR );
    // navigator.notification.alert('Server Error');
    }
    });
});
Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
  • not write success: success: just success: – Mehmet Oct 26 '12 at 11:37
  • try success: function(msg, textStatus, jqXHR)... like Mehmet suggest. If you still have problems take a look to your console, do you get any errors? – F481 Oct 26 '12 at 11:45
  • @F481 Thanks for reply. Am not getting any error in my console. You have any idea. – Gopinath Manickam Oct 26 '12 at 11:58
  • @Mehmet This time also am not getting the result. – Gopinath Manickam Oct 26 '12 at 12:01
  • @Mehmet Thanks for reply.I have edited my question. Please go through my question. Then let me know the answer. – Gopinath Manickam Oct 26 '12 at 12:19
  • you are using asp.net local server with debug your service. Is it running now? I am using IIS for my projects. – Mehmet Oct 26 '12 at 12:23
  • and add contentType: "application/json; charset=utf-8" to jquery ajax method.. – Mehmet Oct 26 '12 at 12:28
  • and look at http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/ this blog. It has great samples with asp.net web service and jquery ajax. – Mehmet Oct 26 '12 at 12:30
  • @Mehmet Thanks for your reply. I have edited my question. Please go through my question and let me know your commands. I have put the alert inside the $.ajax function. But that alert was not working. So that $.ajax function is not working. How can i call that. PLease help me to solve this issue – Gopinath Manickam Oct 26 '12 at 12:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18630/discussion-between-mehmet-and-gopinath-m) – Mehmet Oct 26 '12 at 12:44

1 Answers1

0

It is because you are using "localhost" when you run this code on a device/emulator then "localhost" refers to the device/emulator itself and not the web server that contains your rails API. You'll need to switch "localhost" to the IP address or hostname of your server.

Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74