.net 下用javascript調用webservice

來源:互聯網
上載者:User
javascript|web

  .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 >
<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 >
<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 >
<div id="service" style="behavior:url(webservice.htc)">
</div>
</body>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.