[英]JSON parse error when sending POST request using JavaScript's Fetch API
When I send the following POST request using fetch:当我使用 fetch 发送以下 POST 请求时:
fetch(requestUrl, {
method: 'POST',
headers: {
'id': 'my_id',
'secret': 'my_secret',
'Content-Type': 'application/json'
},
body: {
'username': '123',
'password': '456'
}
})
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data);
})
.catch((error) => {
console.log(error);
})
I get the following error:我收到以下错误:
'JSON parse error: Unrecognized token \'object\': was expecting (\'true\', \'false\' or \'null\'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token \'object\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: java.io.PushbackInputStream@f402d7e; line: 1, column: 9]',
I tried troubleshooting, and the error is caused by my headers
and body
that I'm trying to send with the request.我尝试进行故障排除,该错误是由我试图随请求一起发送的headers
和body
引起的。
Does anyone know what exactly the error is related to?有谁知道这个错误到底与什么有关?
JSON starts either with a { or a [ or a normal ascii char. JSON 以 { 或 [ 或正常的 ascii 字符开头。 You trying to do some kind of Mess and so the JSON parser is confused.你试图做一些混乱,所以 JSON 解析器很困惑。 No valid JSON no fun.没有有效的 JSON 不好玩。 It would be also helpful to show us the first few chars of the thing what you either send or what you receive before it happens.在事情发生之前向我们展示您发送或收到的内容的前几个字符也会很有帮助。 The JSON string HAS to be correct according JS documentation.根据 JS 文档,JSON 字符串必须是正确的。 If you try to send special chars like ]}\,'" inside the data sets it will also mix it up. In this case you has to escape those characters. If nothing help make a base64 string of the values. I just can guess that you has noot escaped the "\" char (should look like \ \ escaped ) or did you mean '\object\'? This looks a bit strange as you show it (wrong ' position at start). Some online JSON decoders also gives more helpful information on errors.The error is in anycase inside the JSON string. If some "header" would be wrong - nothing would be returned.如果您尝试在数据集中发送像 ]}\,'" 这样的特殊字符,它也会将其混淆。在这种情况下,您必须转义这些字符。如果没有任何帮助,可以制作一个 base64 字符串值。我只能猜测你没有逃脱“\”字符(应该看起来像\ \转义)还是你的意思是'\object\'?当你展示它时这看起来有点奇怪(错误的'position在开始)。一些在线JSON解码器也提供有关错误的更多有用信息。无论如何,错误都在 JSON 字符串内。如果某些“标题”出错 - 不会返回任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.