简体   繁体   English

未捕获的 SyntaxError:从本地主机获取时 JSON 输入意外结束

[英]Uncaught SyntaxError: Unexpected end of JSON input when fetching from localhost

When I run the code for the following snippet, it gives me the Uncaught SyntaxError: Unexpected end of JSON input.当我运行以下代码片段的代码时,它会给我 Uncaught SyntaxError: Unexpected end of JSON 输入。 I'm new to coding so I would really love some help.我是编码新手,所以我真的很想得到一些帮助。 Thank you.谢谢你。

  fetch('http://localhost:3000/add-user', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        name: 'User 1'
      })
    }).then(res => {
      return res.json();
    })
    .then(data => alert(data))
    .catch(error => alert(error))

I expected it to show the parsed output but it only gives me the error.我希望它显示已解析的 output 但它只给我错误。

try this one试试这个

 try {
await fetch('http://localhost:3000/add-user', {
  method: 'POST',
  headers: new Headers({
   'Content-Type': 'multipart/form-data',
  }),
 body: {
    name: 'User 1'
  }
})
  .then(response => response.json())
  .then(result => {
   
  });

} catch (error) {} } 赶上(错误){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM