简体   繁体   中英

Posting multiple objects in an ajax call

I have a list of data which is within multiple objects.

Each object has an ID and a status and then the main object has a type and a form id.

The problem I am having is posting result via ajax as it doesn't like the multiple objects.

This is the code I have:

var permissionsData = [];

$(".workflowBox").each(function(index, element) {

var obj = { 
status: $(this).attr("data-user-status"), 
record:$(this).attr("data-user-id")
};

permissionsData.push(obj); 
});

permissionsData.userGroupID = userGroupID;
permissionsData.formID = formID;

var posting =   $.ajax({
        url: "http://www.test.com",
        method: 'post', 
        data: permissionsData
});

How can I wrap/send permission data?

How about changing your array to an object and using jQuery's param function .

var permissionsData = {};

$(".workflowBox").each(function(index, element) {

    var obj = { 
        status: $(this).attr("data-user-status"), 
        record:$(this).attr("data-user-id")
    };

    permissionsData[index] = obj; 
});

permissionsData.userGroupID = userGroupID;
permissionsData.formID = formID;

var posting =   $.ajax({
    url: "http://www.test.com",
    method: 'post', 
    data: $.param(permissionsData)
});

也许您可以使用 json2.js 它可以将对象解析为 json 字符串并将 json 字符串解析为对象您可以使用 JSON.stringify 方法将对象解析为 json 字符串

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM