小编Ric*_*hes的帖子

错误:SyntaxError:在 fetch.then.response 处意外结束 JSON 输入

每次尝试在我的 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)

javascript api post fetch

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

api ×1

fetch ×1

javascript ×1

post ×1