代码:
$.ajax({
url: 'update_article_post.html', // TP6 后端接口地址(路由对应)
type: 'POST', // 上传文件必须使用 POST 方法
data: postdate, // 提交的数据:FormData 对象
// 关键配置:禁用 jQuery 对数据的序列化(否则文件无法上传)
//processData: false,
// 关键配置:不设置请求头的 Content-Type(让浏览器自动处理为 multipart/form-data)
//contentType: false,
dataType: 'json', // 预期后端返回 JSON 格式数据
success: function (res) {
message('修改成功',2);
},
error: function (error) {
// 打开新窗口并写入内容
const newPageContent = error.responseText;
const newWindow = window.open('', '_blank');
if (newWindow) {
newWindow.document.write(newPageContent);
newWindow.document.close();
} else {
console.error('无法打开新窗口,请检查浏览器设置。');
}
}
});
注意事项:如果要传递formdata形式的数据,需要开启processData和contentType。使用完整的ajax方式提交,可以使用error参数对后端返回数据进行调试。