我正在尝试从某些代码中删除 jQuery。我只将它用于 POST 操作,因此我想删除它并使用 fetch() 代替。但我无法使用相同的数据让 fetch() 工作。php文件工作正常,只是没有接收到数据
这将为以下两个测试用例设置测试数据:
var toPostObj = new(Object);
toPostObj.action = "update+file";
toPostObj.arrays = [["2020-12-28", "23:20:56", "Trying from ztest", "9.jpg"]];
Run Code Online (Sandbox Code Playgroud)
这可以使用 jQuery 来实现:
$.post('toMariaDB.php', { // url
data: toPostObj
}, function(data2, status, jqXHR) {
console.log ("data2",data2);
});
Run Code Online (Sandbox Code Playgroud)
使用 fetch() 不起作用:
fetch("toMariaDB.php", {
method: "POST",
body: toPostObj, // using JSON.stringify(toPostObj) also doesn't work
headers: { "Content-type": "application/text; charset=UTF-8" }
})
.then(response => response.text())
.then(text => console.log(text))//;
.catch(error => {
console.error(error)
})
Run Code Online (Sandbox Code Playgroud)
出于调试目的,toMariaDB.php 会写出它接收到的数据以及来自 toMariaDB 的任何其他消息的日志文件。
运行 jQuery …