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

” xmlns:xsi=’'&#8221; +

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



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


      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


    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: Logo

You are commenting using your 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 )

Connecting to %s