Introduction:
In this article,demonstartes how to Call a webservice using AJAX.
Main:
The assembling of the SOAP request and analyzing the result is also possible by building strings that contain the valid XML code and transferring if to the server with the well known XMLHttpRequest Object.
Here is the Sample Code:
// call the server using the SOAP encoding
xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
xmlObj.Open("POST", "http://localhost/NetProgrammingHelp/Service.asmx", true);
xmlObj.setRequestHeader("SOAPAction", "http://www.netprogramminghelp.com/NetProgrammingHelpService/GetString");
xmlObj.setRequestHeader("Content•Type", "text/xml; charset=utf•8");
xmlObj.onreadystatechange = RetrievePrimeFactors;
var soapText = "<?xml version='1.0' encoding='utf•8'?>"
+ "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
+ "<soap:Body>"
+ "<GetString xmlns='http://www.netprogramminghelp.com/NetProgrammingHelpService'>"
+ "<inputText>" + inputText + "</inputText>"
+ "</GetString>"
+ "</soap:Body>"
+ "</soap:Envelope>";
xmlObj.Send(soapText);
...
// the response is inside the <GetStringResult> tag outputText = xmlObj.ResponseText;
p = outputText.indexOf("<GetStringResult>");
if (p > 0) {
outputText = outputText.substr(p+24);
outputText = outputText.substr(0, outputText.indexOf("<"));
} // if
// call the server using the SOAP encoding xmlObj = new ActiveXObject("Microsoft.XMLHTTP"); xmlObj.Open("POST", "http://localhost/NetProgrammingHelp/Service.asmx", true); xmlObj.setRequestHeader("SOAPAction", "http://www.netprogramminghelp.com/NetProgrammingHelpService/GetString"); xmlObj.setRequestHeader("Content•Type", "text/xml; charset=utf•8"); xmlObj.onreadystatechange = RetrievePrimeFactors; var soapText = "<?xml version='1.0' encoding='utf•8'?>" + "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" + "<soap:Body>" + "<GetString xmlns='http://www.netprogramminghelp.com/NetProgrammingHelpService'>" + "<inputText>" + inputText + "</inputText>" + "</GetString>" + "</soap:Body>" + "</soap:Envelope>"; xmlObj.Send(soapText); ... // the response is inside the <GetStringResult> tag outputText = xmlObj.ResponseText; p = outputText.indexOf("<GetStringResult>"); if (p > 0) { outputText = outputText.substr(p+24); outputText = outputText.substr(0, outputText.indexOf("<")); } // if |
Conclusion:
Hope,this helps,
Happy Coding.