zoukankan      html  css  js  c++  java
  • AJAX 传值给后台

    来自网络,自己测试过;可以用

    前台:

     <script type="text/javascript">
                 function CSmethod() {
                     $.ajax({ type: "Post",
                         url: "javaTest.aspx/HelloWorld",
                         contentType: "application/json; charset=utf-8",
                         dataType: "json",
                         data: "{days:2}",
                          success:function (data) { alert(data.d); },
                         error: function (err) { alert(err); }
                     });
                    }
             </script>

    后台:

      [System.Web.Services.WebMethod]

     public static string HelloWorld(Int32 days)
            {
            
               // return "Hello World of CS1";
                return "[{\"nowtime\":\"" + DateTime.Now.AddDays(days).ToShortDateString() + "\"}]";

               
            }

    前台发送数组:

    <script type="text/javascript">
           function AddNewWork() {
             var js_Arr = new Array(); //定义数组
              js_Arr.push("{ name: '张三', salary: '50' }, { name: '李四', salary: '60' }, { name: '王五', salary: '70' }");
                    $.ajax({ type: "Post",
                        url: "MyPage.aspx/AddNewWork",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        data: "{arr_Str:[" + js_Arr + "]}", //传送数组

                        success: function (data) { alert(data.d); },
                        error: function (err) { alert(err); }
                    });
                }

    后台接收数组

      [System.Web.Services.WebMethod]
            public static ArrayList AddNewWork(Dictionary<string, string>[] arr_Str)
            {     
            // return "Hello World of CS1";
               // return "[{\"nowtime\":\"" + DateTime.Now.AddDays(days).ToShortDateString() + "\"}]";
                 ArrayList al = new ArrayList();//创建动态数组       
                al.Add("{张三:'10'}");       
                al.Add("{李四:'20'}");       
                al.Add("{王五:'30'}");     
                al.Add("{赵六:'40'}");      
                return al;//返回动态数组

               
            }

    如果要js_Arr是一个数组,那么可以直接使用

    data: {"str_Fc_Id": js_Arr}

    jQuery会帮助进行序列化。

  • 相关阅读:
    剑指Offer-11.二进制中1的个数(C++/Java)
    剑指Offer-10.矩形覆盖(C++/Java)
    剑指Offer-9.变态跳台阶(C++/Java)
    UVA 1608 Non-boring sequence 不无聊的序列(分治,中途相遇)
    UVA1607 Gates 与非门电路 (二分)
    UVA 1451 Average平均值 (数形结合,斜率优化)
    UVA 1471 Defense Lines 防线 (LIS变形)
    UVA 1606 Amphiphilic Carbon Molecules 两亲性分子 (极角排序或叉积,扫描法)
    UVA 11134 FabledRooks 传说中的车 (问题分解)
    UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
  • 原文地址:https://www.cnblogs.com/wycoo/p/2368005.html
Copyright © 2011-2022 走看看