Calling Web Service from Javascript

Hi,

Now we will discuss how to call web service from javascript.

This example demonstrates of

1. Calling web service method from javascript after page loads completely (async) &

2. This method returns XML string &

3. Displays XML node content div &

4. Between if there is any delay from web service, we need to show loading image or loading text.

Lets get into details

  • Add new Item, select webservice from template & name it as “ServiceOne.asmx”
    • Add  [System.Web.Script.Services.ScriptService] attribute to ServiceOnce class
    • Add below method to ServiceOne class
    • [WebMethod]publicstring GetResponse(string callerName){string txt = string.Empty;txt = “<bookstore><book>”;txt = txt + “<title>Everyday Italian</title>”;txt = txt + “<author>Giada De Laurentiis</author>”;txt = txt + “<year>2005</year>”;txt = txt + “</book></bookstore>”;if (callerName == string.Empty)thrownewException(“Web Service Exception: invalid argument”);Thread.Sleep(5000); // To get response delay, forced to sleepreturnstring.Format(txt.ToString());}  
  • Add Script Manager and add Service reference to default.aspx as shown below

<asp:ScriptManagerID=”ScriptManager1″runat=”server”> <Services><asp:ServiceReferencePath=’~/ServiceOne.asmx’/></Services></asp:ScriptManager>

  • Now lets add DIV, which will show loading text if there is any delay response from Web Service & when there is response from web service it will  display XML NODE content

 <divid=”lblResponse”></div>

  • Now Lets the javascript function code which will call web method & the response from web method can be Success/Completed response, TimeOut response & Error response. Below sendRequest function is called in BodyLoad event, & first statement shows loading text in div until we get response from Web Service

This function pass static string value to web method & response will be either completed, timeout or error. So we define respective function names in web method calls.

ServiceOne is web service

GetResponse is web method defined in ServiceOne service class

function SendRequest() {document.getElementById(“lblResponse”).innerHTML = “Please wait loading……. “;ServiceOne.GetResponse(

“TESTING TO BE ……..”, OnComplete, OnError, OnTimeOut);}

  • On Success/Complete reponse we need to parse XML string & read the Title XML node from XML string & display in lblResponse DIV as shown below

function OnComplete(arg) {//alert(arg);

if (window.DOMParser) {parser = new DOMParser();xmlDoc = parser.parseFromString(arg,

“text/xml”);}

else// Internet Explorer

{

xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);xmlDoc.async = “false”;xmlDoc.loadXML(arg);

}

x = xmlDoc.getElementsByTagName(“title”)[0];y = x.childNodes[0];

txt = y.nodeValue;

document.getElementById(“lblResponse”).innerHTML = txt;}

  • Whenever there is ErrorReponse then below function will be called

function OnTimeOut(arg) {alert(“timeOut has occured”);}

  • Whenever there is TimeOut response below function will be called

function OnError(arg) {alert(“error has occured: “ + arg._message);}

  • Finally call sendRequest JS function in body load event as shown below

<bodyonload=”return SendRequest();”>

  • Now lets execute this & checkout the output