SOAP調用Web Servicehtml
(示例位置:光盤\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;
-
- soapMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
- soapMessage += "<soap:Envelope xmlns:xsi=\"http:
- XMLSchema-instance\""
- + " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\
- "http://schemas.xmlsoap.org/soap/envelope/\">";
- soapMessage += "<soap:Body>";
-
- soapData = "<GetProductPrice xmlns=\"http://tempuri.org/\">";
- soapData += " <ProductId>" + i + "</ProductId>";
- soapData += "</GetProductPrice>";
-
-
- 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:
- GetProductPrice");
- xmlhttp.send(soapMessage);
- alert(soapMessage)
- var x = xmlhttp.responseXML;
- alert('調用結果:'+x.childNodes[1].text);
-
- 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節中的內容。spa