Search code examples
c#javascriptjquerywsdl

Accessing C# webservice via javascript


I'm having a bit of an issue with accessing a C# webservice via JQuery. I have a WSDL and a .svc and I'm not entirely sure of how to access the functions I need. I've read: http://www.andrewrowland.com/article/display/consume-dot-net-web-service-with-jquery/ but it doesn't make any sense where I do not have .asmx pages in my application. I know this is a novice question but I'm truly stuck.


Solution

  • On the server end you can publish a service operation with either a [WebGet] or [WebInvoke] attribute,

    [WebGet]
    public string Get()
    {
        return "Hello, world!"; 
    }
    

    Keep in mind you will also need to use WebHttpBinding for the REST functionality, see this link for details: http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx

    Then on the client side, you can use $.ajax or $.get to call the function,

    $.get("http://localhost/somewcfservice.svc/Get", function (data) {
          alert(data); 
    }
    

    The first parameter is the URI of your service operation, and the second argument is a function you pass to the $.get method as a callback for once you receive the data.

    The above code should produce an alert in the browser that says, "Hello, world!"

    Go here for details: http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx