1

I am developing a native application in Android with PhoneGap. and I have created a Websevice in Visual Studio (c#) that will return an XML fragments. so I have converted the returned XML value into JSON, and have it tested in Mozilla firefox. but when i tried to however invoke the Webservice using AJax with Jquery. i tried encoding an alert message that will return the data of the service. however it seems not to return any data. can anyone please help me on this? thanks

here is my code:

$.ajax({
    type: "POST",
    url: "http://10.0.2.2:49878/a.aspx?p_trxn_type=doLogin&p_phoneNumber="+phoneNumber,
    error: function (XMLHttpRequest, textStatus, errorThrown)
    { 
        alert("error");
    },
    dataType : "json",
    cache:false,
    async:false,
    success: function (ret)
    {
        try {
            var jsonObj = eval('(' + ret + ')');
            alert(jsonObj.Contacts.Contact['@phoneNumber']);
            alert(jsonObj.Contacts.Contact.LastName);
            alert(ret.Contacts.Contact['@phoneNumber'])
            alert(ret.Contacts.Contact.LastName);
        }
        catch(ex) {
            alert(ex.message);
        }
        console.log(ret);
        alert(ret.length);
        alert(ret);
        alert(typeof ret);
        alert("success");
    }
});
David Hedlund
  • 128,221
  • 31
  • 203
  • 222
Newbie
  • 179
  • 1
  • 4
  • 12
  • Are you getting any errors in your console ? Can you please list them ? – sirmdawg Apr 20 '12 at 14:07
  • the alert "Error" is the one im getting. -_- – Newbie Apr 20 '12 at 14:15
  • have it alert the textStatus and errorThrown instead of just "error" Also try using: console.log(errorThrown); console.log(textStatus); – sirmdawg Apr 20 '12 at 16:49
  • still cant get any response from my service. also tried the answer Authman Apatira. still doesnt work. :(, do you have any reference i can look on? tried googling and there are so many suggestions. but i wanna try the one which is already experienced by someone. – Newbie Apr 20 '12 at 17:28

1 Answers1

0

Try adding this code:

$(document).bind("mobileinit", function(){
   $.support.cors = true;
   $.mobile.allowCrossDomainPages = true;
});

So long as your webservice isn't locally hosted, you should be able to access it just fine, re my question here on StackOverflow.

Be sure to also check out http://jquerymobile.com/test/docs/api/globalconfig.html and especially http://jquerymobile.com/test/docs/pages/phonegap.html

Lastly, your error function would much better help you by being defined as:

error: function (XMLHttpRequest, textStatus, errorThrown)
{ 
    alert(textStatus + " " + errorThrown);
}
Community
  • 1
  • 1
Authman Apatira
  • 3,994
  • 1
  • 26
  • 33
  • actually my webservice is locally hosted as of the moment. thats why im using the 10.0.2.2:49878 address. anyhow. will look at the links that you gave me. Many Thanks. – Newbie Apr 20 '12 at 14:54
  • By locally hosted, i meant on the same device the request was originating from (localhost / 127.0.0.1 / the physical android device itself) – Authman Apatira Apr 21 '12 at 02:26