Calling a Web service from JavaScript

Calling a Web service from JavaScript

I am new to Web services, and I need your help to know how to call a Web service (JAX-RPC model or Apache SOAP model) from JavaScript. Do I need any special tools for doing this?

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to keep you informed on recent service-oriented architecture (SOA) and SOA-related topics such as integration, governance, Web services, Cloud and more.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchSOA.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSOA.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

If you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component "WebService" to access a Web service. The "WebService" behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP).

To use the "WebService" behavior, you must attach it to an element using the STYLE attribute, as follows:


<DIV ID="GiveItAName"
STYLE="behavior:url(webservice.htc)"></DIV>


 
A complete example taken from the Microsoft Web site is as follows:

<html> 
<head> 
<script language="JavaScript"> 
var iCallID; function init() 

service.useService 
("http://myserver.com/services/myservice.asmx?WSDL", "servicename"); 

function onmyresult() 

if ((event.result.error)&&(iCallID==event.result.id)) 
  { 
  var xfaultcode = event.result.errorDetail.code; 
  var xfaultstring = event.result.errorDetail.string;    
  var xfaultsoap = event.result.errorDetail.raw; 
// 
Add code to output error information here alert("Error ");   } 
else 
  { 
  service.innerHTML= "The method returned the result: " 
                    + event.result.value; 
  } 

</script> 
</HEAD> 
<body onload="init();"> 
<BR> 
Enter a Value 
<input type='text' id='param1'> 
<BR> 
<button onclick='iCallID = service.servicename.callService ("ProcedureName", param1.value);'>
Call A Web Method
</button> 
<div id="service" 
    style="behavior:url(webservice.htc)" 
    onresult="onmyresult();"> 
</div> 
</body> 
</html> 

 

This was first published in March 2004