Click here to Skip to main content
12,691,453 members (30,197 online)
Click here to Skip to main content
Add your own
alternative version

Stats

10.9K views
6 bookmarked
Posted

Calling Web Services from JavaScript

, 13 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to call We Services from JavaScript.

Calling Web Services from JavaScript allows the power of backend processing in your application without a postback, which results in better user experience. JavaScript by default uses the "JavaScript Object Notation" or JSON to communicate with a server. JSON has a shorter length compared with SOAP, and hence is more efficient. It also conforms better to the JavaScript internal object handling system.

If you open a new web site in Visual Studio 2008, the web.config would contain many new sections to enable JSON so many of the necessary framework would already be in place. To call a Web Service from JavaScript, you need to do the following three steps:

  1. Modify the definition of your Web Service (say in MyWebService.asmx.vb) such that the class has an extra attribute as shown below:
    <System.Web.Script.Services.ScriptService()> _
    Public Class ClassName 
        <WebMethod()> _
        Public Sub MethodName(Parameter As TypeA)
            DoSomething()
        End Sub
    End Class
  2. Use asp:scriptmanager in your ASPX file:
    <asp:scriptmanager ID="ScriptManager1" runat="server">
    <Services>
    <asp:ServiceReference Path="~/service/MyWebService.asmx" />
    </Services>
    </asp:scriptmanager>
  3. Call the Web Service within a JavaScript function like this:
    function CallsWebService(anyParametr ) {
      ...;
      NameSpaceName.ClassName.MethodName(anyParameter, OnWSRequestComplete);
    }

The OnWSRequestComplete() function gets called when the Web Service execution finishes. Implement this function as shown below:

function OnWSRequestComplete(results) {
  if (results != null) {
  }
}

License

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

Share

About the Author

Morris Bahrami
Architect
Australia Australia
I have over 17 years of experience in software development in a variety of fields. For last 7 years the emphasis has been mostly on .NET technology (C# and VB.NET) which includes WPF, WCF and ASP.NET AJAX. Also have SQL Server experience including SSIS and SSRS. My blog (http://morrisbahrami.blogspot.com) has a collection of tips and general info for Microsoft Developers.

You may also be interested in...

Comments and Discussions

 
Generaltips for getting return value of webservice method Pin
lijo011@yahoo.com19-Apr-11 2:46
memberlijo011@yahoo.com19-Apr-11 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170117.1 | Last Updated 13 Apr 2011
Article Copyright 2011 by Morris Bahrami
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid