我想将data2变量发送到我的PHP脚本,但是使用下面给出的代码我在控制器中得到$ _POST的空数组
var data2 = {
donation: $("#donationform").serialize(),
allocation: $("#allocationform").serialize(),
allocationerrors: { msg: validation() },
datatoform: new FormData(this)
};
$.ajax({
url: url,
type: "POST",
dataType: "JSON",
data: data2,
processData: false,
contentType: false,
success: function(data, status) {},
error: function(xhr, desc, err) {}
});
答案 0 :(得分:2)
问题是因为您无法在对象中发送二进制FormData
。你需要反过来做;即。将数据附加到FormData
对象并在请求中发送。试试这个:
var formData = new FormData(this);
formData.append('donation', $("#donationform").serialize());
formData.append('allocation', $("#allocationform").serialize());
formData.append('allocationerrors', JSON.stringify({ msg: validation() }));
$.ajax({
url: url,
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function (data, status) {
},
error: function (xhr, desc, err) {
}
});