To invoke Web Service in .htm file

王朝java/jsp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航