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

首頁 > 編程 > .NET > 正文

.net 下用javascript調用webservice

2024-07-10 13:12:17
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿拉尔市| 高要市| 巍山| 崇明县| 平罗县| 通河县| 清苑县| 仁布县| 盈江县| 山东| 阆中市| 左权县| 海兴县| 绵竹市| 庐江县| 文山县| 浮梁县| 南陵县| 曲沃县| 湄潭县| 民丰县| 榕江县| 新竹市| 惠安县| 台南县| 扶余县| 布拖县| 宽甸| 东乡县| 茂名市| 全州县| 汪清县| 东阳市| 罗定市| 南京市| 江永县| 云梦县| 莒南县| 孟州市| 贵州省| 香港 |