7.6.4 SOAP调用Web Service
7.6.4 SOAP调用Web Service
(示例位置:光盘\code\ch07\ WebAppClient\ JsService4.htm)
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <title>SOAP对调用WebService</title>
- <SCRIPT language="JavaScript">
- function GetHelloWorld_SOAP(i)
- {
- var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
- var soapMessage, soapData, URL;
- //设置SOAP信息
- soapMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
- soapMessage += "<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/\">";
- soapMessage += "<soap:Body>";
- //设置SOAP数据 ---- begin ------
- soapData = "<GetProductPrice xmlns=\"http://tempuri.org/\">";
- soapData += " <ProductId>" + i + "</ProductId>";
- soapData += "</GetProductPrice>";
- //设置SOAP数据 ---- end ------
- soapMessage = soapMessage + soapData + "</soap:Body>";
- soapMessage = soapMessage + "</soap:Envelope>";
- URL = "http://localhost:12074/Service1.asmx"; //WebService地址URL
- xmlhttp.Open("POST",URL, false);
- xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
- xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/
- GetProductPrice");//方法名
- xmlhttp.send(soapMessage);
- alert(soapMessage)//SOAP数据信息
- var x = xmlhttp.responseXML;
- alert('调用结果:'+x.childNodes[1].text);
- //返回调用状态,状态为200说明调用成功,状态为500则说明出错
- alert('状态值:'+xmlhttp.Status);
- alert('状态描述:'+xmlhttp.StatusText);
- }
- </SCRIPT>
- </head>
- <body>
- <INPUT type="button" value="SOAP" onclick="GetHelloWorld_SOAP('001')"
- id="Button1" name="Button1">
- <INPUT type="button" value="异常测试" onclick="GetHelloWorld_SOAP('')"
- id="Button3" name="Button3"><BR><BR>
- <div id="div1"></div>
- </body>
- </html>
具体XMLHTTP的用法和属性可以参考第6.3.8节中的内容。
欢迎添加我的公众号一起深入探讨技术手艺人的那些事!
如果您觉得本文的内容有趣就扫一下吧!捐赠互勉!