来源:https://wwwdev.cn/article/20101223/ajaxpro-usage-summary-ajaxmethod-return-string-int.aspx
ajaxpro返回值类型总结-string,int
返回string或者int都一样,只需要取ajaxpro包装好的对象value就行。测试代码如下
ajaxproStringInt.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajaxprostringint.aspx.cs" Inherits="ajaxproStringInt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>ajaxpro返回值类型总结-string</title> </head> <body> <mce:script type="text/javascript"><!-- function callback(r) { var tp = typeof r.context == 'number' ? r.context : r.context.tp; switch (r.context) { case 1: alert('服务器返回的时间为:' + r.value); break; default: alert(r.context.a + '+' + r.context.b + '=' + r.value); break; } } function callServer(tp) { switch (tp) {//在调用方法的同时传递tp参数,以便在回调函数中通过context属性获取到 case 1: ajaxproStringInt.Now('客户端发送的其他内容1', callback, tp); break; default: ajaxproStringInt.Add(7, 8, callback, { tp: tp, a: 7, b: 8 }); break; //给context传递json对象就可以同时传递多种数据了 } } // --></mce:script> <form id="form1" runat="server"><input type="button" value="调用Now" οnclick="callServer(1)" /> <br ><input type="button" value="调用Add" οnclick="callServer(2)" /></form> </body> </html>
ajaxproStringInt.aspx.cs
ajaxproStringInt.aspx.cs
using System; public partial class ajaxproStringInt : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { AjaxPro.Utility.RegisterTypeForAjax(typeof(ajaxproStringInt)); } /// <summary> /// 带一个参数返回值为string /// </summary> /// <param name="other">其他字符参数</param> /// <returns></returns> [AjaxPro.AjaxMethod] public string Now(string other) { return DateTime.Now.ToString() + "--" + other; } /// <summary> /// 返回值为int的方法 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> [AjaxPro.AjaxMethod] public int Add(int a, int b) { return a + b; } }