Simple Phonegap application to call simple web service

3.5k Views Asked by At

I am a beginner in Phonegap and I made a simple web service in Microsoft Visual studio 2010 with two simple method. I want to call a method from that service from my Phonegap application for android platform. I am using xui library which method xhr is pretty much incomprehensible for me. I have read a lot of posts in that topic but I could not figure out how to do that.

My link to the web service looks like this: http://localhost/testservice/Service1.asmx.

This is my web service code:

[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public int Calculate(int firsNumber, int secundNumber)
    {
        return firsNumber + secundNumber;
    }
}

This is my method which should test that service:

function checkWebService() {
      var url = new "http://localhost/testservice/Service1.asmx?op=HelloWorld";
      x$('#test').xhr(url, {error: function(){alert("failed "+this.responseText)},
      callback: function(){
         alert("Success " + this.responseText);
       }
 });

I call this method on button click and I am always getting alert with the text "Success", without "response text".

Probably my url is wrong, but I do not know which url I suppose to type. This is for "HelloWorld" method without parameters, and I also do not know how to call method with parameters.

Any help or explanation please.

1

There are 1 best solutions below

3
On

Looking at the docs (I'm not familiar with XUI), xhr is for getting a standard web page/JSON web service, where as you are calling a standard web service which will return an XML soap response (they are XML tags not HTML). You would need to parse this response to get just the result you require.

An easy way forward would be to change your web service to return a JSON response, then use eval() in javascript to give you a "typed" view of the object. XUI Example here.

If you're new to this you might find that JQuery has a larger document/community support than XUI.

BTW "Localhost" is a loopback address that essentially means "this" computer, so even it did work it would be trying to connect to the webservice on the mobile device (or emulator) rather than your server which you would typically connect to via a URL.