Calling External WebService using JS

 Sometimes we need to call External webservice using JS, i have written a function to call external webservice

Here is the function, you can modify it according to your requirements

function CallCustomWebService(FunctionName, ParameterNameList, ParameterValueList, ProxyURL)

{//FunctionName-Name of the function to call

//ParameterNameList-Array of Parameter names in your proxy function

//ParameterValueList-Array of Parameter value

//ProxyURL-URL of your proxy

var CallingFunctionURL = “” + FunctionName;

var xml = “<?xml version=’1.0′ encoding=’utf-8′?>” +

“<soap:Envelope xmlns:soap=’'” +

” xmlns:xsi=’'” +

” xmlns:xsd=’’>”+

“<soap:Body>” +

“<” + FunctionName + ” xmlns=’’>”;

for (i = 0; i < ParameterNameList.length; i++) {

xml = xml + “<” + ParameterNameList[i] + “>” + ParameterValueList[i] + “</” + ParameterNameList[i] + “>”;


xml = xml + “</” + FunctionName + “>”;

xml = xml + “</soap:Body></soap:Envelope>”;

xmlHttp = new ActiveXObject(“Msxml2.XMLHTTP”);“POST”, ProxyURL, false);

xmlHttp.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);

xmlHttp.setRequestHeader(“Content-Length”, xml.length);

xmlHttp.setRequestHeader(“SOAPAction”, CallingFunctionURL);


var resultXml = xmlHttp.responseXML;

if (resultXml.text == “Unable to connect to the remote server”)

{alert(“Unable to connect to the remote server”);


var errorCount = resultXml.selectNodes(‘//error’).length;

if (errorCount != 0) {

var msg = resultXml.selectSingleNode(‘//description’).nodeTypedValue;


else {return resultXml;}}

Hope it will help somebody!!!

4 thoughts on “Calling External WebService using JS

    1. mahenderpal Post author


      I have provided JS function in this post, and if your asmx service have methods for insert,update and delete you can call them with required parameters.

    1. mahenderpal Post author

      Hi Serene,
      try to enable “Access data sources across domains” from Internet Options->Security->Custom Level->security setting->miscellaneous section.


Leave a Reply

Your email address will not be published. Required fields are marked *