Feb 8, 2012

Consuming a WCF from JavaScript

This is just to give simple steps to call a WCF from client side.

1) Browse the web service URL. Then you know your web service is available. You will see a page like below one.

2) Now click relevant link in the page. Search for SOAPAction tag relevant to method you wish to use, So that you know the SOAPAction value.

3) Now get the request SOAP envelops.

a) If you have the code of WCF service run the code in Visial Studio that results a pop up of WCF Test Client. Then double click the method you need and pass some values to parameters shown in Formatted tab. Then click the XML tab to grab the Request SOAP envelop. Just omit the Header Tag from the XML you get here.

b) If the web service is provided by some other party, you can simply request the soap envelops.

4) Now we got everything we need, call the relevant method of the Web Service through below code. xmlHttp.ResponseXML will return the result.

        xmlhttp = new XMLHttpRequest();
        xmlhttp.open('POST', 'http://localhost:5555/ISV/PricingWCF/WcfIntermediateService.Service1.svc', false);
        xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
        xmlhttp.setRequestHeader('SOAPAction', 'http://tempuri.org/IService1/GetSalesRate');

        var data = '';
        data += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
        data += '<s:Body>';
        data += '<GetSalesRate xmlns="http://tempuri.org/">';
        data += '<productcode>AEC0023678</productcode>';
        data += '</GetSalesRate>';
        data += '</s:Body>';
        data += '</s:Envelope>';


Click this to see how you call WCF service from plug-in.


  1. This comment has been removed by the author.

  2. Hi Zark,

    I will publish the answer here for the benefit of others too.

    This method will not work for services if its security is tightened. Obviously, if you are consuming a third party webservice they will not be flexible in this regard. If you check, you may identify WSHttpBinding binding type in those kinds of services.

    In such scenarios, what I did was I created my own simple WCF web service that consumes the third party service. Now my ‘Simple’ service is having BasicHttpBinding binding type which allows me to consume this way.

    Unfortunately, there is no better solution.