通过 JSON 传输表单数据



我相信我在某处犯了一个非常基本的错误。

我有一个表格,我想传输到PHP页面。我还想发送一个包含该信息的参数,因此我创建了一个基本的 2D 数组: $fd['api'] ->将参数作为字符串连接 $fd['正文'] - 包含表单数据的>

我正在努力将这个数组"$fd"作为 json 字符串传输,并相信我在某处错误地使用了 javascript 语法,因为我不经常使用 Javascript。

任何帮助将不胜感激。

function admin_statistics_form_send(){
var fd = []
fd['api'] = "refresh_all"
fd['body'] = new FormData(document.getElementById("admin_statistics_form"))
var jsonstring = fd
console.log(jsonstring)
$.ajax({
async: true,
beforeSend: function(){
},
url: "admin_statistics_api.php",
type: "POST",
data: jsonstring,
dataType: "json",
processData: false,  // tell jQuery not to process the data
contentType: false,   // tell jQuery not to set contentType
success: function (data) {
console.log(data)
},
error: function(data) {
console.log(data)
}
})
}

您只想发送 FormData 对象。要添加附加到该对象的其他键/值对,请执行以下操作:

function admin_statistics_form_send(){
var fd = new FormData($("#admin_statistics_form")[0]);
fd.append('api',"refresh_all");

$.ajax({
//async: true, // redundant since it is default and should never use `false`
beforeSend: function(){
},
url: "admin_statistics_api.php",
type: "POST",
data: fd,
dataType: "json",
processData: false,  // tell jQuery not to process the data
contentType: false,   // tell jQuery not to set contentType
success: function (data) {
console.log(data)
},
error: function(data) {
console.log(data)
}
})
}

最新更新