• How to call Web Service using jQuery in ASP.NET

    by  • June 13, 2013 • jQuery and Microsoft .NET, Microsoft ASP.NET • 0 Comments

    I have briefly explained in my previous article what web service is and how to create it and I have also explained how we can consume this web service. This article will explain how to consume web service using jQuery. jQuery is a JavaScript library that is used for less and simple JavaScript coding.

    I will create and consume a web service in same project for this article. You can download complete source code at the bottom. You have to download jQuery and include it in your project.

    • First of all, create a new ASP.NET Web Application
    • From project, add New Item and add WebService
    • You must have following line of code in your WebService file before the web service class. This will allow the web service to be called from client side script.

    • Now right a web method and return some value. Remember, this web method must have [WebMethod] keyword at the top and it must be declared public.

    We have created our web service method. Now let’s see how we can call it using jQuery.

    • Add new web form from project and make sure that jQuery file exists in your project.
    • Add following jQuery reference in your web form according to your path and jQuery version.

    • Add a button and a div in your web form.

    • Now write following code to call your web service method.

    Type must be HTTP POST as HTTP GET cannot be used to call a web service method.

    Set the url of your web service method.

    If there are any parameters required, you have to give these parameters in “data” field. You can specify as many parameters as required.

    contentType and datatype must be set to “application/json” and “json” respectively.




    Namecheap.com
    Avatar of GetCodeSnippet.com

    About

    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply