【发布时间】:2026-01-29 01:35:01
【问题描述】:
我正在尝试使用 fetch() api 发布一些数据,但我不断收到“400 - 错误请求”错误。 我使用 jQuery 的 $.ajax() 方法没有任何问题,我想知道我做错了什么:
数据
let obj = {
id_user: 57,
id_category: 60,
note: 'test'
}
使用 jQuery(工作)
$.ajax({
url: 'http://localhost:3001/api/devices',
type: 'post',
data: obj
}).fail(function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
})
使用 fetch()(不起作用)
fetch('http://localhost:3001/api/devices', {
method: 'post',
body: JSON.stringify(obj)
}).then(function(response){
console.log(response)
}).catch(function(error) {
console.log(error)
})
回应
Response {type: "basic", url: "http://localhost:3001/api/devices", redirected: false, status: 400, ok: false…}
我错过了什么吗?
【问题讨论】: