.net 下用javascript呼叫webservice的話,要用到webservice behavior。下面以一個例子來講解之,比較簡單
1 、首先,要建立一個webservice,例如
<%@ WebService Language="C#" class=MyMath %>
using System;
using System.Web.Services;
public class MyMath {
[WebMethod]
public int add(int a, int b)
{
return a + b;
}
[WebMethod]
public int subtract(int a, int b)
{
return a - b;
}
}
然後發布,先得到其wsdl。
2.首先,我們要下載webbehavior.htc這個檔案(可以到http://msdn.microsoft.com/downloads/samples/internet/behaviors/library/webservice/default.asp .)
去下載,然後放到你的web目前目錄下然後在要呼叫webserice的頁面中,修改如下
<body>
<div id="addservice" style="behavior:url(webservice.htc)"></div>
</body>
這裡我們將div id命名為有意義的名稱,並且指定style為webservice行為。接著,我們要書寫javascript來呼叫webserice了:
首先,我們在javascript中,呼叫其wsdladdservice.useService(" http://localhost/services/math.asmx?WSDL","MyMath ");
使用id.useService(WSDLL路徑,簡單的命名方式);
我們之前設定的id是addservice,為了給客戶端呼叫方便,我們這裡取了名稱,叫做MyMath。而為了確保能正確呼叫webserice,必須在body裡的onload事件裡,馬上載入處理webservice呼叫的javascript,如下
<script language="JavaScript">
function init()
{
addservice.useService(" http://localhost/services/math.asmx?WSDL","MyMath "); }
</script>
<body onload="init()">
<div id="service" style="behavior:url(webservice.htc)">
</div>
</body>
在上面,我們透過webservice行為,首先得到了傳回webservice的wsdl,接下來我們要進行呼叫了,呼叫的格式如下: iCallID = id.FriendlyName.callService([CallbackHandler,] "MethodName", Param1, Param2, . ..);
這裡id是我們在div裡設定的id,而FridndbyName是我們剛才為方面而起的命,這裡就是MyMath了,而CallbackHandler是使用回調函數的過程名,如果無設定的話,則預設是使用onresult所調用的方法來進行處理,下文會講到,而param1,,param2等則是說傳入的參數了,如:
<SCRIPT language="JavaScript">
// All these variables must be global,
// because they are used in both init() and onresult().
var iCallID = 0;
var intA = 5;
var intB = 6;
function init()
{
// Establish the friendly name "MyMath" for the WebServiceURL
service.useService("/services/math.asmx?WSDL","MyMath");
// The following method doesn't specify a callback handler, so onWSresult() is used
iCallID = service.MyMath.callService("add", intA, intB);
}
function onWSresult()
{
// if there is an error, and the call came from the call() in init()
if((event.result.error)&&(iCallID==event.result.id))
{
// Pull the error information from the event.result.errorDetail properties
var xfaultcode = event.result.errorDetail.code;
var xfaultstring = event.result.errorDetail.string;
var xfaultsoap = event.result.errorDetail.raw;
// Add code to handle specific error codes here
}
// if there was no error, and the call came from the call() in init()
else if((!event.result.error) && (iCallID == event.result.id))
{
// Show the arithmetic!
alert(intA + ' + ' + intB + ' = ' + event.result.value);
}
else
{
alert("Something else fired the event!");
}
}
</SCRIPT>
<body onload="init()">
<div id="service" style="behavior:url(webservice.htc)" onresult="onWSresult()">
</div>
</body>
注意,用onresult方式返回的話,要在div部分的onresult中指定處理的方法,這裡是用onWsresult()方法,其中根據返回的信息來判斷是否出錯,出錯的話則顯示。
如果用回呼的話,則如下處理
<SCRIPT language="JavaScript">
// All these variables must be global,
// because they are used in both init() and onResult().
var iCallID = 0;
var intA = 5;
var intB = 6;
function init()
{
// Establish the friendly name "MyMath" for the WebServiceURL
service.useService("/services/math.asmx?WSDL","MyMath");
// The following uses a callback handler named "mathResults"
iCallID = service.MyMath.callService(mathResults, "add", intA, intB);
}
function mathResults(result)
{
// if there is an error, and the call came from the call() in init()
if(result.error)
{
// Pull the error information from the event.result.errorDetail properties
var xfaultcode = result.errorDetail.code;
var xfaultstring = result.errorDetail.string;
var xfaultsoap = result.errorDetail.raw;
// Add code to handle specific error codes here
}
// if there was no error
else
{
// Show the arithmetic
alert(intA + ' + ' + intB + " = " + result.value);
}
}
</SCRIPT>
<body onload="init()">
<div id="service" style="behavior:url(webservice.htc)">
</div>
</body>