javascript 错误:SyntaxError:使用 fetch() 时 JSON 输入意外结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49974346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: SyntaxError: Unexpected end of JSON input when using fetch()
提问by Ben
I am having a play with sending JSON between a Go API server and a React based front end. I am getting the following error:
我正在尝试在 Go API 服务器和基于 React 的前端之间发送 JSON。我收到以下错误:
Error: SyntaxError: Unexpected end of JSON input
错误:SyntaxError:JSON 输入意外结束
It is saying this is occuring at line 25 which is
据说这发生在第 25 行,即
.then(response => response.json())
This is the related function:
这是相关的功能:
postData() {
fetch('stuff/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: 'Bob',
age: 53,
})
})
.then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));
}
After trying some troubleshooting i added the error catching to the function with the "success" and "error" outputs so it would at least stop popping the error page and added some console outputs on the API server to see if the data was being passed.
在尝试了一些故障排除后,我使用“成功”和“错误”输出将错误捕获添加到函数中,因此它至少会停止弹出错误页面并在 API 服务器上添加一些控制台输出以查看数据是否被传递。
Apart from the error occurring, everything seems to be working as expected. The Go API server is receiving the json data and I am able to unmarshal it into a structure and write the data to console just fine so I know the data is being passed. No errors are being thrown on the Go side of the operation.
除了发生错误之外,一切似乎都按预期工作。Go API 服务器正在接收 json 数据,我能够将它解组为一个结构并将数据写入控制台就好了,所以我知道数据正在传递。在操作的 Go 端没有抛出任何错误。
I'm having trouble working out what may be causing the error? Looking for some suggestions on how I may further troubleshoot this or resolve the error.
我无法确定可能导致错误的原因?寻找有关如何进一步解决此问题或解决错误的一些建议。
UPDATE:As Dale Suggested i have put .then(response => console.log(response)) into my code. I replaced the standard .then(response => response.json()) with this code. Below is a screenshot of the console from the chrome dev tools.
更新:正如戴尔建议的那样,我已经将 .then(response => console.log(response)) 放入我的代码中。我用这个代码替换了标准的 .then(response => response.json()) 。下面是来自 chrome 开发工具的控制台屏幕截图。
Also it may be worth noting that the error code does not pop up in this case. Could the error be with the server side go code? Below is the handler for the POST endpoint
另外值得注意的是,在这种情况下不会弹出错误代码。错误可能与服务器端代码有关吗?下面是 POST 端点的处理程序
func handlePostTest(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Print(err.Error())
}
var aPerson Person
json.Unmarshal(body, &aPerson)
fmt.Print(" ", aPerson.Name, " ", aPerson.Age)
}
采纳答案by Niladri
From the error message and HTTP response it's evident that if you are using fetch api and returning HTTP 200 from the server side then you should sent some content like "OK", otherwise it returns empty string and if you use .json()on top the response it results in error as it is not a valid json. If you are not sending any content send HTTP 204 from the server side (no content).
从错误消息和 HTTP 响应中可以明显看出,如果您使用 fetch api 并从服务器端返回 HTTP 200,那么您应该发送一些内容"OK",.json()如它不是有效的 json。如果您不发送任何内容,则从服务器端发送 HTTP 204 ( no content)。
So for your case you should send some content from the server side.
因此,对于您的情况,您应该从服务器端发送一些内容。


