Calling Webservice Using Soap Client Open Source Library

This article describes an open source JavaScript library for calling a webservice using an AJAX call.

This article describes an open source JavaScript library for calling a webservice using an AJAX call. This library basically uses AJAX, which is based on XMLHttpRequest. This library is very lightweight and easy to use, since it uses AJAX for calling the webservice so your UI thread will also not be blocked when you call the webservice.

This library facilitates the calling of the web service by providing a basic method called "SOAPClient.invoke" that takes the following arguments:

  1. Web service URL
  2. Web Method Name
  3. Web Method parameters
  4. Mode of calling

    1. If mode is async then pass true
    2. If mode is sync then pass false
       
  5. Callback method
  6. When we call this method, the following operations are performed by this method:

    1. Get WSDL file
    2. Invoking method with required parameter values by sending SOAP request.
    3. Process the reply of server using WSDL so corresponding JavaScript object can be returned
    4. If call mode is async then Call back method will be invoked otherwise it returns corresponding object.

You can use this library by using the following procedure:

  1. Include soapclient.js in your file. You can get this file from here.
  2. Now you need to write the following code to call the method. Here I am using a typical form of the web service calling that accepts a User class object so just observe how to pass the object as a SOAP parameter.

<script type="text/javascript">       
       
var url = ; ///Webservce URL    
       function User(id, username) {
          
this.Uid = id;
           this.Name = username;
       }
       
function SendSamples_callBack(r) {
          
if (r.constructor.toString().indexOf("function Error()") != -1)
               alert(
"ERROR\r\n\r\n" + r.description + "\r\n\r\n[" + r.number + "]");
          
else
               alert(r.Name);
       }
       
function SendSample4a() {
          
var u = new User(1, "Abhishek");
          
var pl = new SOAPClientParameters();
           pl.add(
"user", u);
           SOAPClient.invoke(url,
"Displayy", pl, true, SendSamples_callBack);
      }
<script>

The While Method in the webservice will be like the following:

[WebMethod]
public string Display(MUser user)
{
    
if (user.Uid> 1)
       
return "Valid";
    
else          
       
return "Invalid"
}

Reference

http://javascriptsoapclient.codeplex.com