Calling Webservice Using Soap Client Open Source Library

Introduction

 
This article describes an open-source JavaScript library for calling a web service 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 web services so your UI thread will also not be blocked when you call the web services. 
 
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 a SOAP request.
    3. Process the reply of server using WSDL so corresponding JavaScript object can be returned
    4. If call mode is async then the Call back method will be invoked otherwise it returns the 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.
  1. <script type="text/javascript">         
  2.        var url = ; ///Webservce URL      
  3.        function User(id, username) {  
  4.            this.Uid = id;  
  5.            this.Name = username;  
  6.        }  
  7.        function SendSamples_callBack(r) {  
  8.            if (r.constructor.toString().indexOf("function Error()") != -1)  
  9.                alert("ERROR\r\n\r\n" + r.description + "\r\n\r\n[" + r.number + "]");  
  10.            else  
  11.                alert(r.Name);  
  12.        }  
  13.        function SendSample4a() {  
  14.            var u = new User(1, "Abhishek");  
  15.            var pl = new SOAPClientParameters();  
  16.            pl.add("user", u);  
  17.            SOAPClient.invoke(url, "Displayy", pl, true, SendSamples_callBack);  
  18.       }  
  19. <script> 
The While Method in the webservice will be like the following:
  1. [WebMethod]  
  2. public string Display(MUser user)  
  3. {  
  4.     if (user.Uid> 1)  
  5.        return "Valid";  
  6.     else            
  7.        return "Invalid";