我们都知道在AjaxPro的方法AjaxPro.Utility.RegisterTypeForAjax(typeof(所在类的类名));会将标记有[Ajax.AjaxMethod]方法注册在客户端。在某项目中,设计模板字段引擎,采用html+jquery实现,这里的数据就难免需要ajax获取,但是团队对于js掌握不一,所以我写了下面辅助类,可以像ajaxpro一样简化ajax的开发。
代码-jQueryInvokeMethodAttribute (此处只做标示方法处理,所以为空):
public class jQueryInvokeMethodAttribute : Attribute
{
}
代码-jQueryAjaxUtility(分注册脚本和调用ajax事件):

为了考虑反射的性能,加入了类级注册脚本方法缓存处理jQueryUtilityCache,具体见demo。
测试:
html:
<div>
<input id="Button1" type="button" value="button" />
</div>
</form>
后台方法注册Page_Load
1:
前台:
{
success: function(e) {
alert(e);
},
dataType: "text"
});
后台:
public static string Test(string str)
{
return "hello:" + str;
}
效果:
2:
前台ajax:
{
success: function(e) {
$.each(e, function(i, n) { alert(n); });
},
dataType: "json"
})
后台:
public static int[] TestArrayJson(int p1, int p2, int p3)
{
return new int[] { p1, p2, p3 };
}
效果:
3:
前台ajax:
{
success: function(e) {
alert(e.key);
alert(e.Value);
},
dataType: "json"
})
后台:
public static Test TestArrayxml(string key,string value)
{
return new Test() { key=key,Value=value};
}
效果:
最后看看FireBug中ajax http头信息:
其他资料jQuery目录: 我jQuery系列之目录汇总
附录:代码下载