Javascript調(diào)用Webservice的多種方法
更新時間:2009年02月06日 02:39:23 作者:
通過xmlhttp+webservice(原始方法)
復制代碼 代碼如下:
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調(diào)用webservice+xmlhttp的實現(xiàn)部分。
復制代碼 代碼如下:
<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"?>';
datadata = 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/">';
datadata = data + '<soap:Body>';
datadata = data + '<SayHelloTo xmlns="http://tempuri.org/">';
datadata = data + '<Name>'+value+'</Name>';
datadata = data + '</SayHelloTo>';
datadata = data + '</soap:Body>';
datadata = 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方法需要發(fā)送的那堆東東可以在webservice的測試頁面中找到,自己拼湊加上對應的參數(shù)就可以。
通過style.behavior來實現(xiàn)的方法(比較簡單)
function getfemale()
{
//第一個參數(shù)是webservice的url,后面是名稱
female.useService("news.asmx?WSDL","news");
//設置一個回調(diào)函數(shù),service返回結果的時候回調(diào);第一個參數(shù)是回調(diào)函數(shù)的名稱,后面的是webservice的參數(shù)
intCallID=female.news.callService(female_result,"getphoto","female"); //這里有兩個參數(shù).....
}
function female_result(result)//回調(diào)函數(shù)
{
if(result.error)
{
female.innerHTML=result.errorDetail.string;
}
else
{
female.innerHTML=result.value; //將webservice返回的結果寫如div中
}
}
頁面顯示部分: <div id="female" style="BEHAVIOR:url(WebService.htc)"></div>
ok,這給我們在靜態(tài)頁調(diào)用動態(tài)的內(nèi)容提供了一種途徑;
這里如果給getfemale()函數(shù)加上定時調(diào)用的話,就是一種無刷新更新頁面的機制了。
缺點是webservice會有一定的延遲,即使是本地的webservice也會比靜態(tài)頁面慢很多,初次打開頁面會感覺很不協(xié)調(diào)。
第二種方法使用了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調(diào)用webservice的文章:
使用 Ajax 調(diào)用 SOAP Web 服務,第 1 部分: 構建 Web 服務客戶機
您可能感興趣的文章:
- 用JavaScript調(diào)用WebService的示例
- 用Jquery訪問WebService并返回Json的代碼
- javascript與webservice的通信實現(xiàn)代碼
- jQuery結合Json提交數(shù)據(jù)到Webservice,并接收從Webservice返回的Json數(shù)據(jù)
- js調(diào)用webservice中的方法實現(xiàn)思路及代碼
- JSP使用Servlet過濾器進行身份驗證的方法
- Nodejs中session的簡單使用及通過session實現(xiàn)身份驗證的方法
- AngularJS身份驗證的方法
- js調(diào)用webservice構造SOAP進行身份驗證
相關文章
Asp.net之TextBox只允許輸入數(shù)字的方法總結
Asp.net之TextBox只允許輸入數(shù)字的方法總結,需要的朋友可以參考一下2013-02-02
GridView控件實現(xiàn)數(shù)據(jù)的修改(第9節(jié))
這篇文章主要介紹了GridView控件實現(xiàn)數(shù)據(jù)的修改,需要的朋友可以參考下2015-08-08
詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理
這篇文章主要介紹了詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12

