每次尝试在我的 API 中使用 POST 方法时,我都会收到此错误。
SyntaxError: Unexpected end of JSON input at fetch.then.response
Run Code Online (Sandbox Code Playgroud)
当我使用 GET 方法时,我会正常获取数据。这是代码:
const teste = () => {
fetch("myURL/test", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
id: 1,
name: "Teste",
amount: 1,
value: 3
})
})
.then(response => response.json()) //Here is the error
.then(data => {
console.log(data);
})
.catch((err)=>console.log(err))}
Run Code Online (Sandbox Code Playgroud)
有人能帮我吗?谢谢你。
编辑:我只是添加这一行来查看日志:
.then(response => console.log(response))
Run Code Online (Sandbox Code Playgroud)
这是我得到的:
Response {
type: "cors",
url: "myURL/test",
redirected: false,
status: 201,
ok: true,
…}
body: (...)
bodyUsed: false
headers: …Run Code Online (Sandbox Code Playgroud)