Click here to Skip to main content
13,199,669 members (62,789 online)
Click here to Skip to main content
Add your own
alternative version


138 bookmarked
Posted 24 Jan 2006

JavaScript SOAP Client

, 24 Jan 2006
Rate this:
Please Sign up or sign in to vote.
Using AJAX to call a Web Service.


A lot of talking about AJAX is taking place here and there; AJAX is the acronym of "Asynchronous JavaScript and XML", a technology based on XMLHttpRequest, which is now supported by all main browsers. The basic idea is quite simple - and not actually a breakthrough - but it allows updating a page following a server request, without reloading the entire set of data. Some examples can be found on GMail or Google Suggest. For additional information about AJAX, you can see Wikipedia.

In this article, we propose a solution based on AJAX that has a great advantage with respect to those commonly found in Internet: calls are made to the Web Services.

This permits:

  1. On the server side, we only have to expose a Web Service with the required methods (instead of generating dynamic pages incorporating data that are based on a custom syntax or on a generic XML).
  2. On the client side, we use the WSDL (Web Service Description Language) to automatically generate a JavaScript proxy class so as to allow using the Web Service return types - that is similar to what Visual Studio does when a Web Reference is added to the solution.

The following diagram shows the SOAP Client workflow for asynchronous calls:

The Client invokes the "SOAPClient.invoke" method using a JavaScript function and specifies the following:

  • Web Service URL (please note that many browsers do not allow cross-domain calls for security reasons).
  • Web method name.
  • Web method parameter values.
  • Call mode (async = true, sync = false).
  • Callback method invoked upon response reception (optional for sync calls).

The "SOAPClient.invoke" method executes the following operations (numbers refer to the previous diagram):

  1. It gets the WSDL and caches the description for future requests.
  2. It prepares and sends a SOAP (v. 1.1) request to the server (invoking method and parameter values).
  3. It processes the server reply using the WSDL so as to build the corresponding JavaScript objects to be returned.
  4. If the call mode is async, the callback method is invoked, otherwise it returns the corresponding object.

Using the code

After having exposed our idea about consuming a Web Service via JavaScript, we only have to analyze the code.

Let's start with the class for the definition of the parameters to be passed to the Web method: "SOAPClientParameters":

function SOAPClientParameters()
    var _pl = new Array();
    this.add = function(name, value) 
        _pl[name] = value; 
        return this; 
    this.toXml = function()
        var xml = "";
        for(var p in _pl)
            if(typeof(_pl[p]) != "function")
                xml += "<" + p + ">" + 


The code simply consists of an internal dictionary (associative array) with the parameter name (key) and the related value; the "add" method allows appending new parameters, while the "toXml" method provides XML serialization for SOAP requests (see "SOAPClient._sendSoapRequest").

Let's define the "SOAPClient" class, which can only contain static methods in order to allow async calls, and the only "public" method within this class: "SOAPClient.invoke".

Note: since JavaScript does not foresee access modifiers - such as "public", "private", "protected", etc. - we'll use the "_" prefix to indicate private methods.

function SOAPClient() {}
SOAPClient.invoke = function(url, method, 
                      parameters, async, callback)
        SOAPClient._loadWsdl(url, method, 
                    parameters, async, callback);
        return SOAPClient._loadWsdl(url, method, 
                   parameters, async, callback);

The "SOAPClient.invoke" method interface is described above; our implementation checks whether the call is async (call result will be passed to the callback method) or sync (call result will be directly returned). The call to the Web Service begins by invoking the "SOAPClient._loadWsdl" method:

SOAPClient._loadWsdl = function(url, method, parameters, async, callback)
    // load from cache?
    var wsdl = SOAPClient_cacheWsdl[url];
    if(wsdl + "" != "" && wsdl + "" != "undefined")
        return SOAPClient._sendSoapRequest(url, method, 
                    parameters, async, callback, wsdl);
    // get wsdl
    var xmlHttp = SOAPClient._getXmlHttp();"GET", url + "?wsdl", async);
        xmlHttp.onreadystatechange = function() 
            if(xmlHttp.readyState == 4)
                SOAPClient._onLoadWsdl(url, method, 
                     parameters, async, callback, xmlHttp);
    if (!async)
        return SOAPClient._onLoadWsdl(url, method, parameters, 
                                    async, callback, xmlHttp);

The method searches the cache for the same WSDL in order to avoid repetitive calls:

SOAPClient_cacheWsdl = new Array();

If the WSDL is not found in the cache (it's the first call in the current context), it is requested from the server using an XMLHttpRequest, according to the required mode (sync or not). Once an answer is obtained from the server, the "SOAPClient._onLoadWsdl" method is invoked:

SOAPClient._onLoadWsdl = function(url, method, 
             parameters, async, callback, req)
    var wsdl = req.responseXML;
    SOAPClient_cacheWsdl[url] = wsdl;
    return SOAPClient._sendSoapRequest(url, method, 
                       parameters, async, callback, wsdl);

A WSDL copy is stored into the cache and then the "SOAPClient._sendSoapRequest" method is executed:

SOAPClient._sendSoapRequest = function(url, method, 
                 parameters, async, callback, wsdl)
    var ns = (wsdl.documentElement.attributes["targetNamespace"] + 
              "" == "undefined") ? 
              "targetNamespace").nodeValue : 
    var sr = 
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<soap:Envelope " +
        "xmlns:xsi=\"\" " +
        "xmlns:xsd=\"\" " +
        "xmlns:soap=\"\">" +
        "<soap:Body>" +
        "<" + method + " xmlns=\"" + ns + "\">" +
        parameters.toXml() +
        "</" + method + "></soap:Body></soap:Envelope>";
    var xmlHttp = SOAPClient._getXmlHttp();"POST", url, async);
    var soapaction = 
      ((ns.lastIndexOf("/") != ns.length - 1) ? ns + "/" : ns) + method;
    xmlHttp.setRequestHeader("SOAPAction", soapaction);
    xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlHttp.onreadystatechange = function() 
            if(xmlHttp.readyState == 4)
                     async, callback, wsdl, xmlHttp);
    if (!async)
        return SOAPClient._onSendSoapRequest(method, 
                    async, callback, wsdl, xmlHttp);

The service namespace is taken out of the WSDL (using different XPath queries for Internet Explorer and Mozilla / FireFox), then a SOAP v. 1.1 request is created and submitted. The "SOAPClient._onSendSoapRequest" method will be invoked upon receiving the server response:

SOAPClient._onSendSoapRequest = function(method, 
                     async, callback, wsdl, req)
    var o = null;
    var nd = SOAPClient._getElementsByTagName(
             req.responseXML, method + "Result");
    if(nd.length == 0)
                     "faultcode").length > 0)
            throw new Error(500, 
        o = SOAPClient._soapresult2object(nd[0], wsdl);
        callback(o, req.responseXML);
        return o;        

The server response is processed looking for faults: if found, an error is raised. Instead, if a correct result is obtained, a recursive function will generate the return type by using the service description:

SOAPClient._soapresult2object = function(node, wsdl)
    return SOAPClient._node2object(node, wsdl);

SOAPClient._node2object = function(node, wsdl)
    // null node
    if(node == null)
        return null;
    // text node
    if(node.nodeType == 3 || node.nodeType == 4)
        return SOAPClient._extractValue(node, wsdl);
    // leaf node
    if (node.childNodes.length == 1 && 
       (node.childNodes[0].nodeType == 3 || 
        node.childNodes[0].nodeType == 4))
          return SOAPClient._node2object(node.childNodes[0], wsdl);
    var isarray = SOAPClient._getTypeFromWsdl(node.nodeName, 
                  wsdl).toLowerCase().indexOf("arrayof") != -1;
    // object node
        var obj = null;
            obj = new Object();
        for(var i = 0; i < node.childNodes.length; i++)
            var p = SOAPClient._node2object(node.childNodes[i], wsdl);
            obj[node.childNodes[i].nodeName] = p;
        return obj;
    // list node
        // create node ref
        var l = new Array();
        for(var i = 0; i < node.childNodes.length; i++)
            l[l.length] = 
              SOAPClient._node2object(node.childNodes[i], wsdl);
        return l;
    return null;

SOAPClient._extractValue = function(node, wsdl)
    var value = node.nodeValue;
           node.parentNode.nodeName, wsdl).toLowerCase())
        case "s:string":            
            return (value != null) ? value + "" : "";
        case "s:boolean":
            return value+"" == "true";
        case "s:int":
        case "s:long":
            return (value != null) ? parseInt(value + "", 10) : 0;
        case "s:double":
            return (value != null) ? parseFloat(value + "") : 0;
        case "s:datetime":
            if(value == null)
                return null;
                value = value + "";
                value = value.substring(0, value.lastIndexOf("."));
                value = value.replace(/T/gi," ");
                value = value.replace(/-/gi,"/");
                var d = new Date();
                return d;                
SOAPClient._getTypeFromWsdl = function(elementname, wsdl)
    var ell = wsdl.getElementsByTagName("s:element");    // IE
    if(ell.length == 0)
        ell = wsdl.getElementsByTagName("element");    // MOZ
    for(var i = 0; i < ell.length; i++)
        if(ell[i].attributes["name"] + "" == "undefined")    // IE
            if(ell[i].attributes.getNamedItem("name") != null && 
               ell[i].attributes.getNamedItem("name").nodeValue == 
               elementname && ell[i].attributes.getNamedItem("type") != null) 
                return ell[i].attributes.getNamedItem("type").nodeValue;
        else // MOZ
            if(ell[i].attributes["name"] != null && 
               ell[i].attributes["name"].value == 
               elementname && ell[i].attributes["type"] != null)
                return ell[i].attributes["type"].value;
    return "";

The "SOAPClient._getElementsByTagName" method optimizes XPath queries according to the available XML parser:

SOAPClient._getElementsByTagName = function(document, tagName)
        return document.selectNodes(".//*[local-name()=\""+ 
                                           tagName +"\"]");
    catch (ex) {}
    return document.getElementsByTagName(tagName);

A factory function returns the XMLHttpRequest according to the browser type:

SOAPClient._getXmlHttp = function() 
            var req = new XMLHttpRequest();
            if(req.readyState == null) 
                req.readyState = 1;
                        req.readyState = 4;
                        if(typeof req.onreadystatechange == "function")
            return req;
            return new ActiveXObject(SOAPClient._getXmlHttpProgID());
    catch (ex) {}
    throw new Error("Your browser does not support XmlHttp objects");

SOAPClient._getXmlHttpProgID = function()
        return SOAPClient._getXmlHttpProgID.progid;
    var progids = ["Msxml2.XMLHTTP.5.0", 
    var o;
    for(var i = 0; i < progids.length; i++)
            o = new ActiveXObject(progids[i]);
            return SOAPClient._getXmlHttpProgID.progid = progids[i];
        catch (ex) {};
    throw new Error("Could not find an installed XML parser");

Points of Interest

By using a single little (less than 10 KB) JavaScript library and, on the server side, simply exposing a Web Service with remote methods, you can use AJAX to create dynamic Web applications with no need for reloading the entire page.

See the on-line demo for an example of usage.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Matteo Casati
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: Problem handling SOAP-response from server Pin
Matteo Casati1-Jun-07 3:14
memberMatteo Casati1-Jun-07 3:14 
GeneralRe: Problem handling SOAP-response from server Pin
tddb692-Jun-07 4:18
membertddb692-Jun-07 4:18 
GeneralNeed Example of how to call the JavaScript Pin
SandB23-May-07 2:25
memberSandB23-May-07 2:25 
GeneralRe: Need Example of how to call the JavaScript Pin
Matteo Casati23-May-07 2:30
memberMatteo Casati23-May-07 2:30 
GeneralMarco sei un figo Pin
Alberto Marabini17-May-07 19:22
memberAlberto Marabini17-May-07 19:22 
GeneralRe: Marco sei un figo Pin
Matteo Casati17-May-07 21:07
memberMatteo Casati17-May-07 21:07 
GeneralRe: Marco sei un figo Pin
Alberto Marabini4-Jun-07 15:26
memberAlberto Marabini4-Jun-07 15:26 
GeneralRe: Marco sei un figo Pin
Matteo Casati4-Jun-07 21:05
memberMatteo Casati4-Jun-07 21:05 
QuestionWeb service built in Java? Pin
Gu Gu9-May-07 19:20
memberGu Gu9-May-07 19:20 
GeneralWCF Pin
Dante R. Otero24-Apr-07 10:20
memberDante R. Otero24-Apr-07 10:20 
QuestionMultiple "pending" requests problem Pin
Taros3-Apr-07 0:54
memberTaros3-Apr-07 0:54 
AnswerRe: Multiple "pending" requests problem Pin
Matteo Casati3-Apr-07 2:07
memberMatteo Casati3-Apr-07 2:07 
GeneralNuSOAP Structure Pin
pgiuseppe8-Mar-07 7:22
memberpgiuseppe8-Mar-07 7:22 
NewsAnother SOAP client solution Pin
Matthias Hertel25-Jan-07 0:29
memberMatthias Hertel25-Jan-07 0:29 
You can find another SOAP client on codeproject at and AJAX web controls built on top of it at
GeneralSimply, Excellent Pin
Typed_stratoCASTer16-Jan-07 14:34
memberTyped_stratoCASTer16-Jan-07 14:34 
GeneralRe: Simply, Excellent Pin
Matteo Casati16-Jan-07 21:24
memberMatteo Casati16-Jan-07 21:24 
GeneralRe: Simply, Excellent Pin
bibiboule26-Mar-07 2:05
memberbibiboule26-Mar-07 2:05 
GeneralWon't work with WCF Pin
nantcom19-Dec-06 8:47
membernantcom19-Dec-06 8:47 
Questiondid not get the Response/Output Pin
jwalin khatri28-Nov-06 5:59
memberjwalin khatri28-Nov-06 5:59 
AnswerRe: did not get the Response/Output Pin
Gator7631-Jan-07 3:04
memberGator7631-Jan-07 3:04 
NewsJavaScript SOAP Client on CodePlex Pin
Matteo Casati27-Oct-06 2:19
memberMatteo Casati27-Oct-06 2:19 
GeneralExcellent Pin
masta chef26-Oct-06 5:25
membermasta chef26-Oct-06 5:25 
GeneralRe: Excellent Pin
Matteo Casati27-Oct-06 2:21
memberMatteo Casati27-Oct-06 2:21 
QuestionHow to secure webmethod? Pin
vinh2b25-Oct-06 17:26
membervinh2b25-Oct-06 17:26 
AnswerRe: How to secure webmethod? Pin
nagual_hsu26-Oct-06 15:56
membernagual_hsu26-Oct-06 15:56 
QuestionIsn't time for a source repository somewhere? Pin
MarcoPolloPollo25-Oct-06 10:08
memberMarcoPolloPollo25-Oct-06 10:08 
AnswerRe: Isn't time for a source repository somewhere? Pin
Matteo Casati25-Oct-06 23:03
memberMatteo Casati25-Oct-06 23:03 
QuestionRead NuSOAP response Pin
simwood20-Oct-06 6:27
membersimwood20-Oct-06 6:27 
AnswerRe: Read NuSOAP response [modified] Pin
Kerem Kacel NBCU23-Oct-06 10:52
memberKerem Kacel NBCU23-Oct-06 10:52 
GeneralBrilliant Pin
Jim Blackler19-Oct-06 7:55
memberJim Blackler19-Oct-06 7:55 
GeneralRe: Brilliant Pin
Matteo Casati27-Oct-06 2:08
memberMatteo Casati27-Oct-06 2:08 
QuestionHow to call ATL Web Service with javascript? Pin
stanley guan17-Oct-06 22:47
memberstanley guan17-Oct-06 22:47 
GeneralIE7 fix Pin
Kerem Kacel NBCU16-Oct-06 11:23
memberKerem Kacel NBCU16-Oct-06 11:23 
QuestionUsing Javascript SOAP Client with gsoap in the server side? Pin
nagual_hsu12-Oct-06 0:42
membernagual_hsu12-Oct-06 0:42 
AnswerRe: Using Javascript SOAP Client with gsoap in the server side? Pin
MarcoPolloPollo25-Oct-06 9:57
memberMarcoPolloPollo25-Oct-06 9:57 
GeneralRe: Using Javascript SOAP Client with gsoap in the server side? Pin
nagual_hsu26-Oct-06 0:22
membernagual_hsu26-Oct-06 0:22 
QuestionHow to set time out? Pin
vinh2b5-Oct-06 23:36
membervinh2b5-Oct-06 23:36 
AnswerRe: How to set time out? Pin
MarcoPolloPollo25-Oct-06 9:43
memberMarcoPolloPollo25-Oct-06 9:43 
GeneralRe: Latest version Demo Page Pin
Dewey22-Sep-06 13:21
memberDewey22-Sep-06 13:21 
GeneralRe: Latest version Demo Page Pin
Matteo Casati5-Oct-06 0:12
memberMatteo Casati5-Oct-06 0:12 
GeneralUsing with Java WS Pin
noomyai3-Sep-06 19:32
membernoomyai3-Sep-06 19:32 
QuestionDemo files: what is .asmx file? Pin
mike00421-Aug-06 1:40
membermike00421-Aug-06 1:40 
AnswerRe: Demo files: what is .asmx file? Pin
Matteo Casati21-Aug-06 1:59
memberMatteo Casati21-Aug-06 1:59 
GeneralRe: Demo files: what is .asmx file? Pin
mike00421-Aug-06 2:11
membermike00421-Aug-06 2:11 
GeneralRe: Demo files: what is .asmx file? Pin
Matteo Casati21-Aug-06 2:16
memberMatteo Casati21-Aug-06 2:16 
QuestionIt is beyond my.. Pin
Sendilkumar.M26-Jul-06 18:44
memberSendilkumar.M26-Jul-06 18:44 
AnswerRe: It is beyond my.. [modified] Pin
Matteo Casati26-Jul-06 21:19
memberMatteo Casati26-Jul-06 21:19 
AnswerRe: It is beyond my.. Pin
MarcoPolloPollo8-Aug-06 7:27
memberMarcoPolloPollo8-Aug-06 7:27 
GeneralNamespace issues with SOAP XML [modified] Pin
JoeInBoston7-Jul-06 7:39
memberJoeInBoston7-Jul-06 7:39 
GeneralRe: Namespace issues with SOAP XML Pin
Daniel Tingstrom5-Mar-07 10:32
memberDaniel Tingstrom5-Mar-07 10:32 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 24 Jan 2006
Article Copyright 2006 by Matteo Casati
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid