zoukankan      html  css  js  c++  java
  • Ajax 传包含集合的JSON

    通过ajax给后台传json对象,当json中含对象集合时,如

    $.ajax({
            url : ,
            type : "POST",
            dataType : "json",
            data: { 
                'year; : ;2006',
                'list' : [
                    {'day': '01', 'type' : 'A'},
                    {'day': '02', 'type' : 'A'},
                ]
                  },
    });

    此种方式在服务器端接收不到data

    解决办法,把list中json转成字符串,服务器端再解析成json.

    前端关键代码

        var detail = "";
        
        if ($(".list").length == 0)
        {
            return false;
        }
        
        $(".list").each(function() {
            var day = $(this).find("input").attr("name");
            var order = $(this).find("input:checked").val();
            if (order == undefined){
                order = '--';
            }
            
            detail += '{ "day" : "' + day + '", "order" : "' + order + '"},';
        });
        
        var jsonstr = "({orders:[" + detail + "]})";
        
        $.ajax({
            url : "",
            type : "POST",
            dataType : "json",
            data: { params: JSON.stringify(eval(jsonstr))},

    后端

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    
    String params = this.getRequest().getParameter("params");
    JSONObject json = JSONObject.fromObject(params);
    JSONArray orders = json.getJSONArray("orders");
    int length = orders.size();
    
    for(int i = 0; i < length; i++){
                JSONObject item = orders.getJSONObject(i);
                item.getString("day")    
            }    


    签名:删除冗余的代码最开心,找不到删除的代码最痛苦!
  • 相关阅读:
    腾讯云 ubuntu 上tomcat加载项目很慢
    ubuntu 中iptables
    linux ssh修改 默认22端口
    jetty 客服端 与服务端
    spring mvc 拦截器
    linux配置iptables(3)
    el取bean 对象属性规则
    mybatis 反射bean规则
    fastjson tojson部分规则
    正则获取参数 分组
  • 原文地址:https://www.cnblogs.com/season2009/p/5613202.html
Copyright © 2011-2022 走看看