我有以下对象,我需要将其转换为ajax帖子的数组。有谁知道如何在javascript / jquery中执行此操作?
[Object, Object, Object, Object]
0: Object
name: "authenticity_token"
value: "d8792991d3941352c00474e3f6b4081bd1d7e818"
1: Object
name: "_method"
value: "POST"
2: Object
name: "email"
value: "sdaasdsa"
3: Object
name: "password"
value: "asddadsadasdsa"
这是我的jquery帖子,我需要将对象作为字段数组:
jQuery.post(formAction, fields).done(function(response) {
console.log(response);
}, "json");
由于 皮特
答案 0 :(得分:1)
我认为你需要以类似的方式迭代对象:
tosend = {};
for(key in object) {
tosend[object[key].name] = object[key].value;
}
答案 1 :(得分:1)
var url;
$.ajax({
type: "POST",
url: url,
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
fields
}),
}).done(function (msg) {
alert("success");
});
答案 2 :(得分:0)
描述:将类似数组的对象转换为真正的JavaScript数组。
var myArray = $.makeArray(myObj);
或者
var myArray = myObj.toArray();
你也可以这样做
var myArray ={};
$.each(myObj, function(name, value){
//console.log(name, value);
myArray [myObj[key].name] = myObj[key].value;
});