.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" ></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 >
<div id="service" >
</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 >
<div id="service" 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 >
<div id="service" >
</div>
</body>
新聞熱點
疑難解答
圖片精選