zoukankan      html  css  js  c++  java
  • Ajax使用PageMethods调用后台方法

    很多东西都不会,从头学起;这里贴使用PageMethods调用后台方法

    在后台上写两个方法,一个是有参,一个无参

          [System.Web.Services.WebMethod]
            [System.Web.Script.Services.ScriptMethod]
            public static object GetStatus()
            {
                return System.DateTime.Now.ToString();
            }

            [System.Web.Services.WebMethod]
            [System.Web.Script.Services.ScriptMethod]
            public static object SetName(string firstName, string lastName)
            {
                return firstName + " " + lastName;
            }

    在脚本上进行调用

    <script language="javascript" type="text/javascript">
           /*
         注意事项: 

        (a)需要调用的服务器端方法必须以System.Web.Services.WebMethod特性进行标记
         (b)需要调用的服务器端方法必须为公共静态方法
         (c)需要调用的服务器端方法应写在.aspx页面(或对应的后台代码文件)中,不应写在用户控件中
           */

           window.setInterval(function () {
               PageMethods.GetStatus(function (result) {
                   if (result) {
                       alert(result);//弹出当前时间
                   }
               });
           }, 3000);

           window.onload = function () {
               PageMethods.SetName("zhang", "jinshan", function (result) {
                   alert(result);//弹出姓名
               });
           }

           //以下的写法是错误的:直接
             PageMethods.SetName("zhang", "jinshan", function (result) {
                   alert(result);//弹出姓名

               });
        </script>

    在表单中需要添加 EnablePageMethods="true"

     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
        
  • 相关阅读:
    Python基础第三天——变量、列表、元组、字典
    Django学习第二天
    开篇:鸡汤
    Python常用的字符串方法总结
    Django学习第一天
    Python基础第二天——if判断、while循环、for循环、Pycharm的使用、python字符串
    Python基础第一天——编程的概念、python的介绍与安装、python的变量、python的运算符
    计算机英语单词记录
    亿图画流程图的小技巧——自定义模板
    sysbench介绍
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/2566135.html
Copyright © 2011-2022 走看看