国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

.net 下用javascript調用webservice

2019-11-18 17:10:34
字體:
來源:轉載
供稿:網友

.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>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南昌县| 普兰县| 张家界市| 资中县| 尉氏县| 西青区| 泸溪县| 清镇市| 共和县| 辽阳县| 聂拉木县| 沁水县| 维西| 南溪县| 鹿邑县| 烟台市| 唐山市| 锡林郭勒盟| 专栏| 磐安县| 乾安县| 陈巴尔虎旗| 杨浦区| 河津市| 中西区| 漯河市| 秀山| 宁都县| 乐业县| 通州区| 休宁县| 易门县| 大庆市| 寿光市| 上饶市| 宁南县| 仙桃市| 湖南省| 闵行区| 前郭尔| 宜州市|