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/'&#8221; +

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

” 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!!!

Advertisements

4 Comments

Filed under MS CRM & Javascript

4 responses to “Calling External WebService using JS

  1. Can i get the source code with an example.Can i acess asmx services can do insert,update ,delete options

    • mahenderpal

      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.

  2. serene

    Hi,

    I got “Permission Denied” when calling external webservice. What can i do to solve this problem?

    • mahenderpal

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s