Asp.net中用 AJAX调用后台静态的方法总结

所属分类: 软件教程 / 编程开发 阅读数: 1059
收藏 0 赞 0 分享

从客户端调用后台静态方法
    1.Ajax Library方式
    C#代码:
    [WebMethod]
    public static DateTime GetCurrentTime(string str)
    {
        return DateTime.Now;
    }
    JS代码:
    <form id="form1" runat="server">
    <script language=javascript type="text/javascript">
        function GetCurrentTime1() {
            PageMethods.GetCurrentTime('NewEgg ajax training', CheckIsSuccess);
        }
        function CheckIsSuccess(result) {
            alert(result);
        }
     </script>
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
        <input id="Button1" type="button" value="客户端控件调用服务器端的方法" onclick="GetCurrentTime1()" />
    </div>
    </form>
    说明:
    C#方法必须加 "[WebMethod]"
    前台页面必须使用引用 服务器控件
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    调用方法: PageMethods.后台方法名(参数[,参数....], 成功后调用的方法名);
Ajax Library
image

image2. jQuery方式    C#代码:
    [WebMethod]
    public static string ABC(string ABC)
    {
        return ABC;
    }
    JS代码:
    $().ready(
            function() {
                $("#AjaxDemo").click(function() {
                    $.ajax({
                        type: "POST",
                        url: "Default.aspx/ABC",
                        data: "{'ABC':'test'}",
                        contentType: "application/json; charset=utf-8",
                        success: function(msg) {alert(msg); }
                    })
                })
            }
        )
    说明: 必须引用jQuery库文件.
    3. 还有一种好像是要引用AJAX.dll文件的. 在后台注册前台方法. 这个好像在.net2.0的时候用的比较多. 具体没仔细研究.
    还望有其他更简单方法的同学互相交流下~

更多精彩内容其他人还在看

eclipse安装pydev插件步骤(图文)

本文主要介绍了在eclipse上安装pydev插件的过程,开发python的朋友可以看一下
收藏 0 赞 0 分享

hadoop入门之通过页面验证hadoop是否安装成功的方法

本文主要介绍了通过页面验证hadoop是否安装成功的方法,大家参考使用吧
收藏 0 赞 0 分享

hadoop入门之验证hdfs是否能够正常运行的方法

本文主要介绍了hadoop验证hdfs是否能够正常运行的方法,大家参考使用吧
收藏 0 赞 0 分享

hadoop入门之统计单词在文件中出现的个数示例

本文主要介绍了hadoop统计单词在文件中出现的个数的示例,大家参考使用吧
收藏 0 赞 0 分享

hadoop入门之hadoop集群验证任务存放在不同的节点上

本文主要介绍了hadoop集群验证任务存放在不同的节点上的方法,大家参考使用吧
收藏 0 赞 0 分享

hadoop入门之设置datanode的心跳时间的方法

本文主要介绍了hadoop设置datanode的心跳时间的方法,大家参考使用吧
收藏 0 赞 0 分享

hadoop入门之hdfs基本操作命令使用方法

本文主要介绍了hadoop的hdfs基本操作命令使用方法,大家参考使用吧
收藏 0 赞 0 分享

windows7下vs2010安装opencv2.4.3详细步骤(图)

本文记录Windows7 X86 SP1操作系统环境下,安装与配置OpenCV2.4.3的详细步骤。前置需求:已安装有VS2010
收藏 0 赞 0 分享

plsql develope连接64位Oracle 11g出错解决方案(图)

PLSQL Developer登录框中的Database下拉框始终空白,解决方法看下面的图吧
收藏 0 赞 0 分享

WinForm开发picturebox图像重绘(picturebox显示图片)代码分享

有多重方式可以实现PictureBox的图像重绘,本文针对一种应用情形,给出一种简便易用的方法来实现PictureBox的图像重绘
收藏 0 赞 0 分享
查看更多