zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    Linux数据库还原备份
    loadrunner遇到的问题
    Maven基础入门与核心知识
    数据去中心化的场景与流程
    设计模式:灵活编程(观察者模式)
    使用Mycat构建MySQL读写分离、主从复制、主从高可用
    设计模式:灵活编程(装饰模式)
    设计模式:灵活编程(组合模式)
    Laravel5:重定向 redirect 函数的详细使用
    设计模式:对象生成(单例、工厂、抽象工厂)
  • 原文地址:https://www.cnblogs.com/chenlh/p/2440854.html
Copyright © 2011-2022 走看看