客户端代理使用细节【函数调用/回调函数的完整签名及WebService级别默认属性】

函数调用完整签名

      Invoke(arg1,…,argN,onSucceeded,onFailed,userContext)

      注:Invoke:调用AJAX的JS方法;

       arg1……argN为函数传递的所有参数;

       onSucceeded:为执行成功后的回调函数;

       onFailed:为执行失败后的回调函数;

       userContext:可用用于在执行函数和回调函数间传递参数,为任意类型;

 

 

回调函数完整签名

       onSucceeded(result,userContext,methodName);

       注:onSucceeded:执行成功后的回调函数;

              Result:服务器返回的值;

              userContext:执行函数传入的userContext变量;

              methodName:执行函数的函数名称;

 

       onFailed(error,userContext,methodName);

        注:onFailed:为执行失败调用的回调函数;

        error:错误处理类的一个实例,存储的为出错信息;

        userContext:执行函数传入的userContext变量;

        methodName:执行函数的函数名称;

 

 

WebService级别默认属性

       注:设置此属性后可不必在执行函数的方法体内再次标明

       timeout;//默认超时时间,值小于0表示不限时间

       defaultUserContext;//设置默认的userContext;

       defaultSucceededCallback;//设置默认成功的回调函数;

       defaultFailedCallback;//设置默认失败的回调函数;

-------------------------------------------------------------------------------------------------------------------------------------------

实例:

aspx页面

<asp:ScriptManager ID="ScriptManager1" runat="server">
		<Services>
			<asp:ServiceReference Path="ErrorHandling.asmx" />
		</Services>
	</asp:ScriptManager>
		
	<input type="button" value="getDivision" οnclick="getDivision(5, 0)" />
	<input type="button" value="timeout" οnclick="timeout()" />
	
	<script language="javascript" type="text/javascript">
		ErrorHandling.set_defaultFailedCallback(failedCallback);//设置默认处理失败回调函数
		ErrorHandling.set_timeout(2000);//设置默认超时时间
			
		function getDivision(a, b)
		{
			ErrorHandling.GetDivision(a, b);
		}
			
		function timeout()
		{
			ErrorHandling.Timeout();
		}
			
		function failedCallback(error, userContext, methodName)
		{
			var message = String.format(
				"Timeout: {0}\nMessage: {1}\nExceptionType: {2}\nStackTrace: {3}",
				error.get_timedOut(),
				error.get_message(),
				error.get_exceptionType(),
				error.get_stackTrace());
			
			alert("Error at " + methodName + "\n\n" + message);
		}
	</script>


 

ErrorHandling.asmx页面代码

<%@ WebService Language="C#" Class="ErrorHandling" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Threading;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ErrorHandling  : System.Web.Services.WebService
{
	[WebMethod]
	public int GetDivision(int a, int b)
	{
		return a / b;
	}

	[WebMethod]
	public int Timeout()
	{
		Thread.Sleep(5000);
		return 0;
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值