1. 程式人生 > >js呼叫Web Service方式

js呼叫Web Service方式

  1. <!doctype html>  
  2. <html lang="en">  
  3.  <head>  
  4.   <meta charset="UTF-8">  
  5.   <title>qq線上測試</title>  
  6.   <script type="text/javascript">  
  7.     function getqq(){  
  8.         //建立XMLHttpRequest物件  
  9.         var xhr = new XMLHttpRequest(); 
  10.         var url = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx"

  11.         //開啟連線  
  12.         xhr.open("post",url,true);  
  13.         //設定資料型別  
  14.         xhr.setRequestHeader("content-type","text/xml;charset
    =utf-8");  
  15.         //設定回撥函式  
  16.         xhr.onreadystatechange=function(){  
  17.             //判斷是否傳送成功和判斷服務端是否響應成功  
  18.             if(4 == xhr.readyState && 200 == xhr.status){  
  19.                 alert(xhr.responseText);  
  20.             }  
  21.         }  
  22.         //組織SOAP協議資料  
  23.         var soapXML = "<?xml version="1.0" encoding="utf-8"?>"  
  24.         +"<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/">"  
  25.             +"<soap:Body>"  
  26.             +"<qqCheckOnline xmlns="http://WebXml.com.cn/">"  
  27.                 +"<qqCode>"+document.getElementById("number").value+"</qqCode>"
  28.             +"</qqCheckOnline>"  
  29.           +"</soap:Body>"  
  30.         +"</soap:Envelope>";  
  31.         alert(soapXML);  
  32.         //傳送資料  
  33.         xhr.send(soapXML);  
  34.     }  
  35.   </script>  
  36.  </head>  
  37.  <body>  
  38.   qq號查詢:<input type="text" id="number"/> <input type="button" value="查詢" onclick="javascript:getqq();"/>  
  39.  </body>  
  40. </html>