简体   繁体   English

使用 JavaScript 的 Fetch API 发送 POST 请求时 JSON 解析错误

[英]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.我尝试进行故障排除,该错误是由我试图随请求一起发送的headersbody引起的。

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.

相关问题 编辑:无法使用 Fetch API 向 Express 服务器发送 POST 请求,不断收到 JSON.parse 错误 - EDITED: Can't send POST request to Express server with Fetch API, keep getting JSON.parse error 使用JavaScript Fetch API执行POST请求 - Using the javascript fetch api to perform a POST request 在 fetch API 上使用 Content-Type "application/javascript" 发送 post 请求后没有预期的响应被发送 - Response not expected after sending a post request using Content-Type "application/javascript" on fetch API is sent JSON 解析错误:针对获取 API 请求的意外标识符“数组” - JSON Parse error: Unexpected identifier "Array" against Fetch API request 使用Fb登录按钮(JAVASCRIPT)时POST https://api.parse.com/1/用户400(错误请求)错误 - POST https://api.parse.com/1/users 400 (Bad Request) ERROR when using Fb login button (JAVASCRIPT) 使用 JavaScript 向 WCF 服务发送 POST 请求时出现 400 Bad Request 错误 - 400 Bad Request error when sending POST request to WCF service using JavaScript 使用JavaScript将帖子发送到api时跨域请求阻止错误 - Cross-Origin Request Blocked error on sending post to an api using javascript Blogger JSON API Post fetch和Content Parse - Blogger JSON API Post fetch and Content Parse 使用 fetch 向 Golang 服务器发送 post 请求 - sending post request using fetch to Golang server 向控制器发送JSON字符串化javascript对象时发生POST 500错误 - POST 500 error when sending JSON stringified javascript object to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM