Top
首页 > 老文章 > 正文

To invoke Web Service in .htm file

To invoke Web Service in .htm file

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

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=\"http://www.w3.org/2001/XMLSchema-instance\" "+
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "+
" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" "+
 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
"<soap:Body>"+
" <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>"+
"</soap:Body>"+
"</soap:Envelope>";

//ready to send SOAP,need a little change
xmlhttp.Open("POST",url,false);
xmlhttp.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
xmlhttp.setRequestHeader("HOST","127.0.0.1");
xmlhttp.setRequestHeader("Content-Length",SoapRequest.length);
xmlhttp.setRequestHeader("SOAPAction", "someNamespace");
xmlhttp.Send(SoapRequest);

//If the SOAP has been successfully
alert("ok");
}

//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");
 xmlDOC.load(xmlhttp.responseXML);
 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;");
 document.write(xmlStr);

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

</script>

//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();">
<hr>
</div>

Some Notes:
You can add a DIV like:

<div id=result>
</div>

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

Conclusion

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.

加载更多

专题访谈

合作站点
stat