Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Webservice Using Javascript.

5.00/5 (6 votes)
27 Aug 2010CPOL 29.1K  

Introduction


This Little Code just gives you hands on trick to access Webservice using Javascript.


Background


I had knew that webservice can be accessed by Javascript but haven't thought that much eeasy way. Have a Look at it.


Using the Code


Lets Start Step By Step.


Step 1


Add Script Manager Instance to your ASPX Page, Here i have specified path of my webservice (QuoteService.asmx).


<asp:ScriptManager ID="_ScriptManager" runat="server">
            <Services>
                <asp:ServiceReference Path="~/QuoteService.asmx" />
            </Services>
        </asp:ScriptManager>

Step 2


Add Below Mentioned Attribute to your code behind of Webservice (in my case QuoteService.cs)


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]

And Write Some method to your Webservice Code Behind Class . ( In my case QuoteService.cs)


[WebMethod]
public string GetQuote(int intVar)
{
return (intVar + 6).ToString();
}

Step 3


Call a Function from Javascript and define the Return value handler of function.


function LookUp() {
            QuoteService.GetQuote(2, OnLookupComplete)
        }

Define result Handler Function as Depicted in above code


function OnLookupComplete(result) {
            alert(result);
        }

So Guyz We are done with our Tricks.


Points of Interest


I just was thinking it will be to time taking concept to calling webservice from javascript but as i searched through net . i found very simple solution, so i thought may it will be useful to you also in some part of code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)