jQuery and Ajaxpro.Net 结合使用

转自:http://hi.baidu.com/czh0221/blog/item/0f2ca44545888921cefca375.html
 
相关 Ajaxpro(AjaxPro.2.dll )引用这里不再多说:喜欢直接点.
AjaxproTest.aspx.cs
namespace jQueryAjaxApro
{
    public partial class AjaxproTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //注册AjaxPro(这里不用注册)
           // AjaxPro.Utility.RegisterTypeForAjax(typeof(jQueryAjaxApro.AjaxproTest));
            if (!IsPostBack)
            {

            }
        }

        //可被客户端调用的方法
        [AjaxPro.AjaxMethod]
        public string GetTest()
        {
            return DateTime.Now.ToString("yyyy年MM月dd日");
        }

        [AjaxPro.AjaxMethod]
        public string HelloWorld(string s)
        {
            return "HelloWorld" + s;
        }
    }
}

AjaxproTest.aspx
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>测试AjaxPro</title>
    <script type ="text/javascript" src="/js/lib/jquery.js"></script>
    <script type="text/javascript" defer="defer">
        jQuery(function($) {
            // 你可以在这里继续使用$作为别名...
            
            //--通过jquery Ajax获得.ajaxpro方法(带参数传递)
            $.ajax({
                type: "POST", 
                url: "/ajaxpro/jQueryAjaxApro.AjaxproTest,jQueryAjaxApro.ashx", //发送请求的地址
                data: '{"s":" Michael"}', //发送到服务器的数据
                beforeSend: function(xhr) { //发送请求前可修改 XMLHttpRequest 对象的函数
                    xhr.setRequestHeader("ajaxpro-method", "HelloWorld");
                },
                success: function(s) {
                    alert(s);
                }
            });

            //--第二种方式(不带参数传递)
            MyAjaxClass = function() { };
            MyAjaxClass.prototype.url = "";
            MyAjaxClass.prototype.method= "";
            MyAjaxClass.prototype.execute= function(onSucces, onError) {
                return $.ajax({
                    type: "POST",
                    url: this.url,
                    data: "{}",
                    beforeSend: function(xhr) {
                        xhr.setRequestHeader("ajaxpro-method", 
method);
                    },
                    success: function(s) {
                        var o = null;
                        eval("o=" + s + ";");
                        if (o != null && typeof(o) != "undefined" && typeof onSucces == "function") 
                                onSucces(o);
                        if (typeof onError == "function")
                               onError({ "Message": "Failed." });
                    }
                });
            }
             //绑定
            $("#btnTest").click(function() {
                var ajaxMyClass = new MyAjaxClass();
                ajaxMyClass.url = "/ajaxpro/jQueryAjaxApro.AjaxproTest,jQueryAjaxApro.ashx";
                ajaxMyClass.method = "GetTest";
                ajaxMyClass.method(function(x) { $("#txtContent").val(x); }, null);
            });
        }); 

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div >
       <div>测试AjaxPro方法调用:如下</div>
       <span><input type="button" id="btnTest" value="获得服务器数据" />&nbsp;&nbsp;<input type="text" readonly ="readonly" id ="txtContent" /></span>
    </div>
    </form>
</body>
</html>

原文地址:https://www.cnblogs.com/chenlh/p/2440854.html