Javascript調用Webservice的多種方法 .

來源:互聯網
上載者:User
[c-sharp] view plaincopyprint?
  1. using System;  
  2. using System.Web;  
  3. using System.Web.Services;  
  4. using System.Web.Services.Protocols;  
  5.   
  6. [webservice(namespace = "http://tempuri.org/")]  
  7. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  8. public class Service : System.Web.Services.WebService  
  9. {  
  10.     public Service ()   
  11.     {  
  12.         //uncomment the following line if using designed components    
  13.         //InitializeComponent();    
  14.     }  
  15.   
  16.     [webmethod]  
  17.     public string SayHelloTo(string Name)   
  18.     {  
  19.         return "Hello "+Name;  
  20.     }  
  21. }  

using System;<br />using System.Web;<br />using System.Web.Services;<br />using System.Web.Services.Protocols;</p><p>[webservice(namespace = "http://tempuri.org/")]<br />[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]<br />public class Service : System.Web.Services.WebService<br />{<br />public Service ()<br />{<br />//uncomment the following line if using designed components<br />//InitializeComponent();<br />}</p><p>[webmethod]<br />public string SayHelloTo(string Name)<br />{<br />return "Hello "+Name;<br />}<br />}<br /> 

 

還是俗了點。:)

2. js調用webservice+xmlhttp的實現部分。

[xhtml] view plaincopyprint?
  1. <html>  
  2.     <title>Call webservice with javascript and xmlhttp.</title>  
  3. <body>  
  4.     <mce:script language="javascript"><!--  
  5.    
  6.   
  7.     //test function with get method.  
  8.     function RequestByGet(data){   
  9.         var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
  10.         //Webservice location.  
  11.         var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";  
  12.         xmlhttp.Open("GET",URL, false);   
  13.         xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");   
  14.         xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");   
  15.         xmlhttp.Send(data);   
  16.         var result = xmlhttp.status;   
  17.         //OK  
  18.         if(result==200) {   
  19.             document.write(xmlhttp.responseText);   
  20.         }   
  21.         xmlhttp = null;   
  22.     }   
  23.   
  24.     //test function with post method  
  25.     function RequestByPost(value)  
  26.     {  
  27.         var data;  
  28.         data = '<?xml version="1.0" encoding="utf-8"?>';   
  29.         data = data + '<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/">';   
  30.         data = data + '<soap:Body>';   
  31.         data = data + '<SayHelloTo xmlns="http://tempuri.org/">';   
  32.         data = data + '<Name>'+value+'</Name>';   
  33.         data = data + '</SayHelloTo>';   
  34.         data = data + '</soap:Body>';   
  35.         data = data + '</soap:Envelope>';   
  36.   
  37.         var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
  38.         var URL="http://localhost:1323/WebSite6/Service.asmx";  
  39.         xmlhttp.Open("POST",URL, false);   
  40.         xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");   
  41.         xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");   
  42.         xmlhttp.Send(data);   
  43.         document.write( xmlhttp.responseText);   
  44.     }  
  45.   
  46.       
  47. // --></mce:script>  
  48.   
  49.     <input type="button" value="CallWebserviceByGet" onClick="RequestByGet(null)">  
  50.     <input type="button" value="CallWebserviceByPost" onClick="RequestByPost('Zach')">  
  51. </body>  
  52. </html>  

<html><br /><title>Call webservice with javascript and xmlhttp.</title><br /><body><br /><mce:script language="javascript"><!--</p><p>//test function with get method.<br />function RequestByGet(data){<br />var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");<br />//Webservice location.<br />var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";<br />xmlhttp.Open("GET",URL, false);<br />xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");<br />xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");<br />xmlhttp.Send(data);<br />var result = xmlhttp.status;<br />//OK<br />if(result==200) {<br />document.write(xmlhttp.responseText);<br />}<br />xmlhttp = null;<br />} </p><p>//test function with post method<br />function RequestByPost(value)<br />{<br />var data;<br />data = '<?xml version="1.0" encoding="utf-8"?>';<br />data = data + '<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/">';<br />data = data + '<soap:Body>';<br />data = data + '<SayHelloTo xmlns="http://tempuri.org/">';<br />data = data + '<Name>'+value+'</Name>';<br />data = data + '</SayHelloTo>';<br />data = data + '</soap:Body>';<br />data = data + '</soap:Envelope>'; </p><p>var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");<br />var URL="http://localhost:1323/WebSite6/Service.asmx";<br />xmlhttp.Open("POST",URL, false);<br />xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");<br />xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");<br />xmlhttp.Send(data);<br />document.write( xmlhttp.responseText);<br />}</p><p>// --></mce:script></p><p><input type="button" value="CallWebserviceByGet" onClick="RequestByGet(null)"><br /><input type="button" value="CallWebserviceByPost" onClick="RequestByPost('Zach')"><br /></body><br /></html>

 

對於使用post方法需要發送的那堆東東可以在webservice的測試頁面中找到,自己拼湊加上對應的參數就可以。

通過style.behavior來實現的方法(比較簡單)

原文地址:http://www.zahui.com/html/4/37953.htm

 

[javascript] view plaincopyprint?
  1. function getfemale()  
  2. {   
  3.     //第一個參數是webservice的url,後面是名稱   
  4.     female.useService("news.asmx?WSDL","news");  
  5.     //設定一個回呼函數,service返回結果的時候回調;第一個參數是回呼函數的名稱,後面的是webservice的參數   
  6.     intCallID=female.news.callService(female_result,"getphoto","female"); //這裡有兩個參數.....   
  7. }  
  8.   
  9. function female_result(result)//回呼函數   
  10. {  
  11.     if(result.error)  
  12.     {  
  13.         female.innerHTML=result.errorDetail.string;  
  14.     }  
  15.     else  
  16.     {  
  17.         female.innerHTML=result.value; //將webservice返回的結果寫如div中   
  18.     }  
  19. }   

function getfemale()<br />{<br />//第一個參數是webservice的url,後面是名稱<br />female.useService("news.asmx?WSDL","news");<br />//設定一個回呼函數,service返回結果的時候回調;第一個參數是回呼函數的名稱,後面的是webservice的參數<br />intCallID=female.news.callService(female_result,"getphoto","female"); //這裡有兩個參數.....<br />}</p><p>function female_result(result)//回呼函數<br />{<br />if(result.error)<br />{<br />female.innerHTML=result.errorDetail.string;<br />}<br />else<br />{<br />female.innerHTML=result.value; //將webservice返回的結果寫如div中<br />}<br />}  
頁面顯示部分: <div id="female" style="BEHAVIOR:url(WebService.htc)"></div>

 

ok,這給我們在靜態頁調用動態內容提供了一種途徑;
這裡如果給getfemale()函數加上定時調用的話,就是一種無重新整理更新頁面的機制了。
缺點是webservice會有一定的延遲,即使是本地的webservice也會比靜態頁面慢很多,初次開啟頁面會感覺很不協調。

 

第二種方法使用了style.代碼就簡潔多了他使用了css.定義了div的行為.比起第一種方法,就易讀多了:)

style="behavior:url(webservice.htc)"

前提條件是:

if you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component "WebService" to access a Web service. The "WebService" behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP).

附註:另一個總結文章在:http://goody9807.cnblogs.com/archive/2005/08/17/216725.html

 

calling WebServices using Javascript

if you are using Microsoft IE 5 or later, you can use the behavior/HTML-Component "WebService" to access a Web service. The "WebService" behavior communicates with Web services over HTTP using Simple Object Access Protocol (SOAP).

to use the "WebService" behavior, you must attach it to an element using the STYLE attribute, as follows:

 

style="behavior:url(webservice.htc)">

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.