Good2Know: Calling .NET Web Service via Javascript (Client side)

The .NET framework makes it very simple to accomplish a task that is not in the least trivial.  What client-side calls to a web service provides is the ability to retrieve data and make it available to the user without a page refresh.  So let me get right to the point and show you how it’s done.

Place a ScriptManager within the Form tag

<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>

<Services>

<asp:ServiceReference Path=”http://localhost/YourService/databroker.asmx&#8221; />

</Services>

</asp:ScriptManager>

The web service will be called by one JavaScript method and its result returned to a return method.  The GetWebServiceData method will call the web service, pass the parameters the service receives and also pass the method that the web service will call to return its results.

function GetWebServiceData() {

var myName;

myName = “Danny”

MyWebServiceNamespace.MyWebService.HelloWorldMethod(myName, WebServiceReturn);

}
function WebServiceReturn(result) {

document.getElementById(“resultReturn”).innerHTML = result;

}

The applications are endless and chances are you have already seen this in action countless of times.  This opens the door to much richer and interactive data driven applications.