I am really confused as to how can i consume a webservice using javascript. Some websites use the html control consumption method and some are calling it directly on the tutorials that they have listed. None of the method seems to be working for me. Would
someone explain me how can i consume an asp.net 2.0 web service using client side javascript embedded in an HTML page with a simple example.
Here is the code that normally I use to call webservices. The variables are soapHeader, postUrl and soapActionUrl. When you expore the webservices lets say
http://localhost:4174/TestWeb/WebService.asmx?op=HelloWorld it will give you some description, you can find the value of postUrl and soapActionUrl there only, this is for SOAP 1.1, if
you have some other then this create the soapHeader as described. Following is the code is calling HelloWorld webmethod which is in WebService.asmx.
sultansaadat
Member
3 Points
14 Posts
Consuming asp.net webservice using javascript!
Aug 07, 2008 08:54 AM|LINK
Hi,
I am really confused as to how can i consume a webservice using javascript. Some websites use the html control consumption method and some are calling it directly on the tutorials that they have listed. None of the method seems to be working for me. Would someone explain me how can i consume an asp.net 2.0 web service using client side javascript embedded in an HTML page with a simple example.
Thanks & Regards,
Sultan
bhadelia.imr...
Contributor
3191 Points
533 Posts
Re: Consuming asp.net webservice using javascript!
Aug 07, 2008 11:58 AM|LINK
Hi,
Here is the code that normally I use to call webservices. The variables are soapHeader, postUrl and soapActionUrl. When you expore the webservices lets say http://localhost:4174/TestWeb/WebService.asmx?op=HelloWorld it will give you some description, you can find the value of postUrl and soapActionUrl there only, this is for SOAP 1.1, if you have some other then this create the soapHeader as described. Following is the code is calling HelloWorld webmethod which is in WebService.asmx.
<script language="javascript" type="text/javascript">
var soapHeader = '<?xml version="1.0" encoding="utf-8"?>'soapHeader +=
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'soapHeader +=
'<soap:Body><HelloWorld xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>' //the url of Webservice var postUrl = 'http://localhost:4174/TestWeb/WebService.asmx';
var soapActionUrl = 'http://tempuri.org/HelloWorld';
var xmlhttp = null; try { xmlhttp = new XMLHttpRequest();}
catch (e) {xmlhttp = false; } try {xmlhttp =
new ActiveXObject('Msxml2.XMLHTTP'); }catch (e) { xmlhttp = false; } try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');}catch (e) { xmlhttp = false; }alert(xmlhttp);
if( xmlhttp ){
xmlhttp.open (
'POST', postUrl, true); xmlhttp.onreadystatechange = function(){
if( xmlhttp.readyState == 4 ){
alert(xmlhttp.responseText);
}
};
xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');xmlhttp.setRequestHeader(
"Host", "localhost"); xmlhttp.setRequestHeader("SOAPAction", soapActionUrl); xmlhttp.setRequestHeader("Content-Length", soapHeader.length );xmlhttp.send(soapHeader);
}
</script>
Hope this helps you.[MCTS]
Born to fly High
http://knowledgebaseworld.blogspot.com/
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: Consuming asp.net webservice using javascript!
Aug 07, 2008 12:02 PM|LINK
Here are a couple of good articles with sample source to get you started.
http://www.codeproject.com/KB/webservices/callWebServiceUsingJS.aspx
http://www.codeguru.com/vb/vb_internet/webservices/article.php/c7781__3/
NC...