ajaxpro ajaxmethod 重载调用问题
ajaxpro使用总结系列其他内容
ajaxpro综合示例-ajaxpro无刷新更新gridview数据
ajaxPro7.7.31.1 出现this.onTimeout is not a function 的Bug解决方案
ajaxPro7.7.31.1 返回DataTable,DateSet出错
ajaxpro无刷新,分页更新repeater/GridView数据源
ajaxpro ajaxmethod 重载调用问题
使用ajapro时,ajaxmethod最好不好有重载版本,要不由于js变量的非强类型性,导致调用重载版本时会出现调用不到对应版本的函数,或者出现错误,即使传递了回调函数也会出现同步执行等问题。
下面是测试代码
第一,多个参数的重载,调用到不同的函数或者发生错误,异步转变为同步
+展开
-C#
/// <summary>
/// 无参数返回值为string
/// </summary>
/// <returns></returns>
//[AjaxPro.AjaxMethod]
public string Now()
{
return DateTime.Now.ToString()+"--Params0";
}
/// <summary>
/// 带一个参数返回值为string
/// </summary>
/// <param name="other">其他字符参数</param>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public string Now(string other)
{
return DateTime.Now.ToString() + "--" + other;
}/// <summary>
/// 带一个参数返回值为string
/// </summary>
/// <param name="other">其他字符参数</param>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public string Now(string other,string other1)
{
return DateTime.Now.ToString() + "--" + other+"--"+other1;
}
/// 无参数返回值为string
/// </summary>
/// <returns></returns>
//[AjaxPro.AjaxMethod]
public string Now()
{
return DateTime.Now.ToString()+"--Params0";
}
/// <summary>
/// 带一个参数返回值为string
/// </summary>
/// <param name="other">其他字符参数</param>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public string Now(string other)
{
return DateTime.Now.ToString() + "--" + other;
}/// <summary>
/// 带一个参数返回值为string
/// </summary>
/// <param name="other">其他字符参数</param>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public string Now(string other,string other1)
{
return DateTime.Now.ToString() + "--" + other+"--"+other1;
}
+展开
-JavaScript
function callback(r){alert('回调:'+r.value);}
alert(className.Now(callback).value);//虽然传递了回调函数,但是实际是同步执行。输出null,因为发生错误,r.error = {"Message":"类型“System.DBNull”的对象无法转换为类型“System.String”。","Type":"System.ArgumentException"
alert(className.Now('客户端发送的其他内容1', callback).value);//虽然传递了回调,但是还是同步的,没有执行回调函数的输出。只要一次输出“回调:服务器时间+客户端发送的其他内容1”
alert(className.Now('客户端发送的其他内容1', '客户端发送的其他内容2', callback, tp));//异步执行,先输出null【注意这里没有调用value属性,因为异步执行,所以ajaxpro包装的对象会为null,这样对null获取value属性会出错】,后面输出“回调:服务器时间+客户端发送的其他内容1”,忽略了参数“客户端发送的其他内容2”。
alert(className.Now(callback).value);//虽然传递了回调函数,但是实际是同步执行。输出null,因为发生错误,r.error = {"Message":"类型“System.DBNull”的对象无法转换为类型“System.String”。","Type":"System.ArgumentException"
alert(className.Now('客户端发送的其他内容1', callback).value);//虽然传递了回调,但是还是同步的,没有执行回调函数的输出。只要一次输出“回调:服务器时间+客户端发送的其他内容1”
alert(className.Now('客户端发送的其他内容1', '客户端发送的其他内容2', callback, tp));//异步执行,先输出null【注意这里没有调用value属性,因为异步执行,所以ajaxpro包装的对象会为null,这样对null获取value属性会出错】,后面输出“回调:服务器时间+客户端发送的其他内容1”,忽略了参数“客户端发送的其他内容2”。
第二,参数类型不一样的重载版本,会出现错误。
+展开
-C#
/// <summary>
/// 参数为int
/// </summary>
/// <param name="i">其他字符参数</param>
/// <returns></returns>
//[AjaxPro.AjaxMethod]
public string Now(int i)
{
return DateTime.Now.ToString() + "--" + i.ToString();
}
/// <summary>
/// 带一个参数返回值为string
/// </summary>
/// <param name="other">其他字符参数</param>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public string Now(string other)
{
return DateTime.Now.ToString() + "--" + other;
}
/// 参数为int
/// </summary>
/// <param name="i">其他字符参数</param>
/// <returns></returns>
//[AjaxPro.AjaxMethod]
public string Now(int i)
{
return DateTime.Now.ToString() + "--" + i.ToString();
}
/// <summary>
/// 带一个参数返回值为string
/// </summary>
/// <param name="other">其他字符参数</param>
/// <returns></returns>
[AjaxPro.AjaxMethod]
public string Now(string other)
{
return DateTime.Now.ToString() + "--" + other;
}
+展开
-JavaScript
function callback(r){alert('回调:'+r.value);}
className.Now(1,callback);//输出null,因为发生错误, r.error = {"Message":"类型“System.Int64”的对象无法转换为类型“System.String”。","Type":"System.ArgumentException"
className.Now('客户端发送的其他内容1', callback);//正常调用,输出“回调:服务器时间+客户端发送的其他内容1”
className.Now(1,callback);//输出null,因为发生错误, r.error = {"Message":"类型“System.Int64”的对象无法转换为类型“System.String”。","Type":"System.ArgumentException"
className.Now('客户端发送的其他内容1', callback);//正常调用,输出“回调:服务器时间+客户端发送的其他内容1”
通过上面的示例说明,最好不要有重载版本的ajaxmethod。
加支付宝好友偷能量挖...
原创文章,转载请注明出处:ajaxpro ajaxmethod 重载调用问题