【发布时间】:2021-07-17 03:49:19
【问题描述】:
您好我想通过Ajax在WordPress中发送名称和图像文件等信息,但只发送标题并且图像文件没有保存在$_post中。我也想发几张图。我的代码是:
var data_image = new FormData();
$("#add_doc").click(function () {
var sr = $('#blah').attr('src');
var title = $('#title_picture').val();
// console.log(sr);
$('#theDiv').append('<img width="140px" height="140px" class="attach_file" id="theImg" src="' + sr + '" />')
.append('<input type="hidden" name="title_file[]" value="' + title + '"> ');
var props = $('#imgInp')[0].files[0];
var tile_image = $("#title_picture").val();
data_image.append(tile_image, props);
for (var pair of data_image.entries()) {
console.log(pair[0] + ', ' + pair[1]);
}
});
$("#save").click(function () {
var data_s3 = new FormData();
for (var vv of data_image.entries()) {
data_s3.append("action", "save_data_step_three");
data_s3.append("title_file",vv[0] );
data_s3.append("image", vv[1]);
console.log(vv[1]);
for (var pair of data_s3.entries()) {
console.log(pair[0] + ', ' + pair[1]);
}
$.ajax({
url: "/wp-admin/admin-ajax.php",
"headers": {
"Accept":"application/json",
},
"mimeType": "multipart/form-data",
type: "post",
data: data_s3,
cache: false,
enctype: 'multipart/form-data',
contentType: false,
processData: false,
method: 'POST',
success: function (response) {
console.log(response)
}
});
}
});
在我发送数据表单之前,我会检查它。一切看起来都是正确的。但是当它发送时,没有收到值。
价值 $_post:
{action: "save", title_file: ""}
action: "save"
title_file: ""
【问题讨论】: