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

    转自http://youzhangcai.blog.163.com/blog/static/166848184201082393511143/

    / 需要标识为WebMethod 
    [System.Web.Services.WebMethod]
    // 注意,要让前台调用的方法,一定要是public和static的 
    public static string aaa(string name)
    {
        string result = "Hello:" + name;
        return result;
    }


     <script type="text/javascript">
            function btnClick(){
                // 调用页面后台方法,前面跟方法所需的参数,接着是方法回调成功时要执行的js函数,最后一个是方法回调失败时要执行的js函数
                PageMethods.aaa("you",funReady,funError);
            }        
            // result 就是后台方法返回的数据
            function funReady(result){
                alert(result);
            }
            // err 就是后台方法返回的错误信息
            function funError(err){
                alert("Error:" + err._message );
            }
         </script>


     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
            <input type="button" onclick="btnClick()" value="test" />

     方法2:
       
        function Show()
        {
          var v = "中国";
          var s = '<%=CsharpVoid("'+v+'") %>';  // 你好!“+V+”
          alert(s);
        }


        protected string CsharpVoid(string strCC)
        {
           strCC = "你好!" + strCC;
           return strCC;
        }

     
    <input type="button" onclick="Show()" value="hhhh" />

    方法三:

    // 需要标识为WebMethod 
    [System.Web.Services.WebMethod]
    // 注意,要让前台调用的方法,一定要是public和static的 
    public static string aaa(string name)
    {
        string result = "Hello:" + name;
        return result;
    }

    <script type="text/javascript">
            function btnClick(){
                // 调用页面后台方法,前面跟方法所需的参数,接着是方法回调成功时要执行的js函数,最后一个是方法回调失败时要执行的js函数
                WebSerCustomer.aaa("you",function(ress){//ress就是后台方法返回的数据,aaa是webservice WebSerCustomer.axms页面上的方法
              alert(ress)

              });
            }        
              </script>

    <asp:ScriptManager ID="ScriptManager1" runat="server">
           <Services><asp:ServiceReference Path="~/WebSerCustomer.asmx" /></Services>//WebSerCustomer.asmx后台webservice类的页名称
       </asp:ScriptManager>

     <input type="button" onclick="btnClick()" value="test" />

  • 相关阅读:
    UVALive 5983 MAGRID DP
    2015暑假训练(UVALive 5983
    poj 1426 Find The Multiple (BFS)
    poj 3126 Prime Path (BFS)
    poj 2251 Dungeon Master 3维bfs(水水)
    poj 3278 catch that cow BFS(基础水)
    poj3083 Children of the Candy Corn BFS&&DFS
    BZOJ1878: [SDOI2009]HH的项链 (离线查询+树状数组)
    洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)
    洛谷P3065 [USACO12DEC]第一!First!(Trie树+拓扑排序)
  • 原文地址:https://www.cnblogs.com/liziqiang/p/3578231.html
Copyright © 2011-2022 走看看