Javascript調用Webservice的彙集

整理了一下:js調用WebService的幾種方法:

通過XmlHttp+WebService(原始方法)

原文地址:http://netboy.cnblogs.com/archive/2006/02/18/333260.html

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string SayHelloTo(string Name) {
return "Hello "+Name;
}

}
還是俗了點。:)

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

<html>
<title>
Call webservice with javascript and xmlhttp.
</title>
<body>
<script language="javascript">

//Test function with get method.
function RequestByGet(data){

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
//Webservice location.
var URL="http://localhost:1323/WebSite6/Service.asmx/SayHelloTo?Name=Zach";
xmlhttp.Open("GET",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
var result = xmlhttp.status;
//OK
if(result==200) {
document.write(xmlhttp.responseText);
}
xmlhttp = null;
}

//Test function with post method
function RequestByPost(value)
{
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
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/">';
data = data + '<soap:Body>';
data = data + '<SayHelloTo xmlns="http://tempuri.org/">';
data = data + '<Name>'+value+'</Name>';
data = data + '</SayHelloTo>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';

var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
var URL="http://localhost:1323/WebSite6/Service.asmx";
xmlhttp.Open("POST",URL, false);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=gb2312");
xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/SayHelloTo");
xmlhttp.Send(data);
document.write( xmlhttp.responseText);

}

</Script>

<input type="button" value="CallWebserviceByGet" onClick="RequestByGet(null)">
<input type="button" value="CallWebserviceByPost" onClick="RequestByPost('Zach')">

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

通過Style.BEHAVIOR來實現的方法(比較簡單)

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

<script language="javascript">
function getfemale()
{
//第一個參數是webservice的url,後面是名稱
female.useService("news.asmx?WSDL","news");
//設置一個回調函數,service返回結果的時候回調;第一個參數是回調函數的名稱,後面的是webservice的參數
intCallID=female.news.callService(female_result,"getphoto","female"); //這裏有兩個參數.....
}

function female_result(result)//回調函數
{
if(result.error)
{
female.innerHTML=result.errorDetail.string;
}
else
{
female.innerHTML=result.value; //將webservice返回的結果寫如div中
}
}
</script>
頁面顯示部分: <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)">

 附上IBM上面有關Ajax調用WebService的文章:

使用 AJAX 調用 SOAP Web 服務,第 1 部分: 構建 Web 服務客戶機

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章