代码:
$.ajax({
url: 'bg2_post.html', // TP6 后端接口地址(路由对应)
type: 'POST', // 上传文件必须使用 POST 方法
data: {'img':img}, // 提交的数据:FormData 对象
// 关键配置:禁用 jQuery 对数据的序列化(否则文件无法上传)
//processData: false,
// 关键配置:不设置请求头的 Content-Type(让浏览器自动处理为 multipart/form-data)
//contentType: false,
dataType: 'json', // 预期后端返回 JSON 格式数据
success: function (res) {
if (res == 'success') {
message('设置成功', 2);
setTimeout(() => {
$("#right_content").load('sethomebg.html');
}, 2500);
}
},
error: function (error) {
// 打开新窗口并写入内容
const newPageContent = error.responseText;
const newWindow = window.open('', '_blank');
if (newWindow) {
newWindow.document.write(newPageContent);
newWindow.document.close();
} else {
console.error('无法打开新窗口,请检查浏览器设置。');
}
}
});
说明: dataType的类型默认为'json',如果为“html”则影响接收判断。