首页 > 老文章 > 正文

To invoke Web Service in .htm file

To invoke Web Service in .htm file

To invoke Web Service in .htm file
发布时间:2006-03-08 22:31        来源:        作者:

With some ActiveXObject, we can invoke web service in the pure html, also need help of VB Script or JavaScript.

Here, i want to use javascript to show an example:

The first part a to send the SOAP message:

<script language="javascript1.2">

//creat a Object to send SOAP
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

//the function used to send request
function sendRequest(){

//also can use other services
var url = "http://localhost:8080/axis/services/TestService";

var SoapRequest="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+

 //add required nameSpace
"<soap:Envelope xmlns:xsi=\"\" "+
"xmlns:xsd=\"\" "+
" xmlns:soapenc=\"\" "+
" <q1:test xmlns:q1='"somenamespace">"+

//you can add the SOAP message manually here
//you also can use Script to get the input string from form, like document.form1.content.value

"  </q1:test>"+

//ready to send SOAP,need a little change
xmlhttp.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", "someNamespace");

//If the SOAP has been successfully

//the function for receive the SOAP
function receiveResponse(){

//the SOAP has been sent , the status will set to 200
 if(xmlhttp.status == 200){

//create a Object to solve XML  
 var xmlDOC = new ActiveXObject("MSXML.DOMDocument");
 xmlStr = xmlDOC.xml;

//here i just to print the SOAP content, you can read the XML with the MSXML.DOMDocument
//for more detail about how to operate, you can find information from its guide when installed

 xmlStr = xmlStr.replace(/</g,"&lt;");
 xmlStr = xmlStr.replace(/>/g,"&gt;");

  xmlDOC = null;
  alert("fail in sending SOAP");


//the next step is to add some simple input field and button
<div align="center">
search Content<input name="content" type="text"><input name="go" type="button" value="Go" onClick="sendRequest();receiveResponse();">

Some Notes:
You can add a DIV like:

<div id=result>

and use result.innerHTML=... to display the result returned by web service


We you have finished them, you can invoke yout webservice by click the button.

The way to invoke web service is just a example on tech. But it is not suggected in used in your site, since the visited can see all the code include the SOAP detail, which may cause security problem. Instead, you can copy the code to asp in the <%%>. the code will be compile can can not be seen. Through the example, i see how web service is powerful and useful.