Transmitting a plurality of data objects to the front end of the background

1. The front end of the data string array package into json

$.fn.serializeJson = function(){
    var jsonData1 = {};
    var serializeArray = this.serializeArray();
    // 先转换成{"id": ["12","14"], "name": ["aaa","bbb"], "pwd":["pwd1","pwd2"]}这种形式
    $(serializeArray).each(function () {
        if (jsonData1[this.name]) {
            if ($.isArray(jsonData1[this.name])) {
                jsonData1[this.name].push(this.value);
            } else {
                jsonData1[this.name] = [jsonData1[this.name], this.value];
            }
        } else {
            jsonData1[this.name] = this.value;
        }
    });
    // 再转成[{"id": "12", "name": "aaa", "pwd":"pwd1"},{"id": "14", "name": "bb", "pwd":"pwd2"}]的形式
    var vCount = 0;
    // 计算json内部的数组最大长度
    for(var item in jsonData1){
        var tmp = $.isArray(jsonData1[item]) ? jsonData1[item].length : 1;
        vCount = (tmp > vCount) ? tmp : vCount;
    }

    if(vCount > 1) {
        var jsonData2 = new Array();
        for(var i = 0; i < vCount; i++){
            var jsonObj = {};
            for(var item in jsonData1) {
                jsonObj[item] = jsonData1[item][i];
            }
            jsonData2.push(jsonObj);
        }
        return JSON.stringify(jsonData2);
    }else{
        return "[" + JSON.stringify(jsonData1) + "]";
    }
};

2.

var data2 = $("#delivered_info_add_form").serializeJson();
$.ajax({
    url:urls.add,
    type:"post",
    data:data2,//List直接接收
    //data:{'jsonStr':data2},//字符串接收     contentType: 'application / json; charset = utf-8', // header information setting request, List to be set directly received    success:function (data) {
        if(data.success){
            view.msg(data.message);
            actives.reload();
            layer.close(index)
        }else {
            view.msg(data.message)
        }
    }
});

3. Background receiving json string objects into json

List receives directly or background

Published 68 original articles · won praise 19 · views 240 000 +

Guess you like

Origin blog.csdn.net/tyjlearning/article/details/101291674