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 = “http://tempuri.org/” + FunctionName;

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

“<soap:Envelope xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/'” +

” xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance'” +

” xmlns:xsd=’http://www.w3.org/2001/XMLSchema’>”+

“<soap:Body>” +

“<” + FunctionName + ” xmlns=’http://tempuri.org/’>”;

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”);

xmlHttp.open(“POST”, ProxyURL, false);

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

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

xmlHttp.setRequestHeader(“SOAPAction”, CallingFunctionURL);

xmlHttp.send(xml);

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

      Hi,

      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.

      Reply ↓
    1. mahenderpal Post author

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

      Reply ↓

Leave a Reply

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

Are you human? Please solve:Captcha