How to Call Custom Web service through JavaScript in Asp.Net.

 In this post I am going to Show How we can create a web Service and consume it through javascript i.e Calling a web service in Javascript using Asp.Net/ Calling a WebService in Javascript in Microsoft Dynamics CRM.  
   
 1. First of all create a web service by going to Visual Studio-> Select New Website-> WebService -> Service.cs  
 2. Create a new WebMethod or update your HelloWorld WebMethod as below, I have create a sample Web service which accepts 2 parameters and based on that returns a string value:   
   
   string[,] stocks = { { "Infy", "Infosys", "122.35" }, { "DSS", "Direction", "98.22" } };  
   
   [WebMethod]  
   public string getName(string symbol, string Name)  
   {  
     for (int i = 0; i < stocks.GetLength(0); i++)  
     {  
       if (String.Compare(symbol, stocks[i, 0], true) == 0 || String.Compare(Name, stocks[i + 1, 0], true) == 0)  
       {  
         return ("Stock Value of " + stocks[i, 1] + " is " + stocks[i, 2]).ToString();  
       }  
     }  
     return "Stock not found";  
   }  
   
 3. Build your WebService and Publish It. Copy your URL which is required while consuming it through JavaScript.  
 4. Now, Your Web srevice is ready and Ready to use. So Create a new Solution where you want to consume above service.   
 5. I have taken a website and in Default.aspx page put your code as below, in this method I have created a link button which fires the JavaScript Method:  
   
 <asp:LinkButton id="lnkSelectComp" onclientclick="return callFunction();" text="Get Value" runat="server" xmlns:asp="#unknown" />  
   
 6. Add a new Javascript file in your solution and add below code in Default.aspx in the Head section for Javascript file source:   
   
 <script src="JavaScript.js" type="text/javascript"></script>  
   
 7. In your JavaScript file add below code first which contains Parameters names, values, WebService url, etc.  
   
 function callFunction() {  
   var FunctionName = "getName";  
   var WebServiceUrl = "http://localhost:88/Service.asmx";  
   var ParameterNameList = new Array("symbol", "Name");  
   var ParameterValueList = new Array("Infy", "Infosys");  
   
   CallCustomWebService(FunctionName, ParameterNameList, ParameterValueList, WebServiceUrl)  
 }  
   
 8. Below is the code for Calling Custom Web Service which takes parameters name, values, url, function Name and return value from Custome Web Service create above.  
   
 function CallCustomWebService(FunctionName, ParameterNameList, ParameterValueList, ProxyURL) {  
   
   var CallingFunctionURL = "http://tempuri.org/" +FunctionName;  
   
   var xml = '<?xml version="1.0" encoding="utf-8"?>' +  
         '<soap:Envelope ' +  
           'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +  
           'xmlns:api="http://127.0.0.1/Integrics/Enswitch/API" ' +  
           'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +  
           'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +  
           '<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 != null || resultXml.text != 'undefined') {  
     alert(resultXml.text);  
   }  
   
   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; }  
 }  
    

Comments

Popular posts from this blog

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

Dumps for Microsoft Dynamics CRM MB2-703 Practice Exam Questions Free

How to import CSV files into DataTable in C#

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

go to top image