When loading my page in Google Chrome, I get a vague error in the console:
当我在谷歌Chrome上加载我的页面时,我在控制台上有一个模糊的错误:
Uncaught SyntaxError: Unexpected end of input
未捕获的语法错误:输入的意外结束
I have no idea what is causing it. How would I go about debugging this error?
我不知道是什么引起的。如何调试这个错误呢?
12 个解决方案
#1
193
This particular error is one annoying fact about V8. In most cases your JavaScript is broken in some way. For example missing a }
or something like that.
这个特别的错误是V8的一个令人讨厌的事实。在大多数情况下,您的JavaScript会以某种方式被破坏。比如丢失一个}之类的东西。
Example given, this will yield "Unexpected end of input" too:
例如,这也将产生“意想不到的输入端”:
eval('[{"test": 4}') // notice the missing ]
But the root cause of the problems seems to be that the requested JSON url has a Content-Type of text/html
which Chrome apparently tries to parse as HTML, which then results in the unexpected end of input due to the fact that the included image tags are being parsed.
但问题的根源似乎在于,请求的JSON url有一个内容类型的文本/html, Chrome显然试图将其解析为html,然后由于包含的图像标签被解析而导致了输入的意外结束。
Try setting the Content-Type to text/plain
I think it should fix the issues.
尝试将内容类型设置为text/plain我认为它应该解决问题。
Nonetheless, V8 could do a better Job about telling one exactly where the input ended unexpectedly.
尽管如此,V8可以更好地告诉一个输入意外终止的确切位置。
#2
67
Try Firebug for Mozilla - it will show the position of the missing }
.
尝试为Mozilla的Firebug -它将显示丢失的}的位置。
http://getfirebug.com/
#3
28
See my case on another similar question:
请参见我关于另一个类似问题的案例:
In my case, I was trying to parse an empty JSON:
在我的例子中,我试图解析一个空JSON:
JSON.parse(stringifiedJSON);
In other words, what happened was the following:
换句话说,所发生的情况如下:
JSON.parse("");
#4
10
I get this error when I have ommitted a closing brace character (}
)in JavaScript code. Check that your braces are properly balanced.
当我省略了JavaScript代码中的闭括号字符(})时,就会出现这个错误。检查你的牙套是否平衡。
#5
9
For the record, for anyone trying to find the various causes for this error. An empty HTML5 data attribute
对于记录来说,对于任何试图找出这个错误的各种原因的人来说。一个空的HTML5数据属性
data-mydata = ''
causes the error too. You should check when the data value is a null string and not include the attribute at all. It goes without saying this is largely relevant to script generated HTML.
导致错误。您应该检查数据值何时为空字符串,而根本不包含属性。不用说,这很大程度上与脚本生成的HTML有关。
#6
8
The issue for me was that I was doing $.ajax with dataType: "json"
for a POST request that was returning an HTTP 201 (created) and no request body. The fix was to simply remove that key/value.
对我来说,问题是我在做$。使用dataType的ajax:“json”用于返回HTTP 201(创建)且没有请求主体的POST请求。解决方法是简单地删除该键/值。
#7
6
JSHint is good at finding the location of missing brackets or bad syntax.
JSHint擅长查找丢失的括号或错误语法的位置。
#8
5
Another cause of this error: if your API intentionally responds with no response body, but responds with a 200 OK
status code instead of a 204 No Content
status code. Some JavaScript libraries may not respond well to unexpected content types when there is no content, so use the correct status code!
这一错误的另一个原因是:如果您的API故意响应没有响应主体,而是使用200个OK状态码而不是204个no内容状态码。当没有内容时,一些JavaScript库可能不能很好地响应意外的内容类型,因此使用正确的状态代码!
#9
2
There will definitely be an open bracket which caused the error.
肯定会有一个左括号导致错误。
I'd suggest that you open the page in Firefox, then open Firebug and check the console – it'll show the missing symbol.
我建议您打开Firefox中的页面,然后打开Firebug并检查控制台——它将显示丢失的符号。
Example screenshot:
示例截图:
#10
0
I faced similar problem using ui bootstrap directive for angularjs - uib-datepicker, when pressing am/pm toggle.
当我按am/pm切换时,我遇到了类似的问题,使用angularjs - uibm -datepicker的ui引导指令。
Error in event handler for (unknown): SyntaxError: Unexpected end of JSON input angular timepicker
事件处理程序(未知)中的错误:SyntaxError: JSON输入角时间选择器的意外端
Turned out it was because of plugin 'Trans-over' (which translates a word when it is clicked). Maybe my answer will help someone, because I didn't found anything on the internet.
原来,这是因为插件“Trans-over”(当它被点击时,翻译为一个单词)。也许我的答案会对某些人有所帮助,因为我在网上找不到任何东西。
#11
0
Since it's an async operation the onreadystatechange
may happen before the value has loaded in the responseText, try using a window.setTimeout(function () { JSON.parse(xhr.responseText); }, 1000);
to see if the error persists? BOL
由于它是一个异步操作,所以onreadystatechange可能在该值加载到responseText之前发生,请尝试使用窗口。setTimeout(函数(){ JSON.parse(xhr.responseText);},1000);查看错误是否持续?波尔
#12
0
My problem was with Google Chrome cache. I tested this by running my web application on Firefox and I didn't got that error there. So then I decided trying emptying cache of Google Chrome and it worked.
我的问题是谷歌Chrome缓存。我通过在Firefox上运行我的web应用程序测试了这一点,我在那里没有发现错误。于是我决定试着清空谷歌Chrome的缓存,它成功了。
#1
193
This particular error is one annoying fact about V8. In most cases your JavaScript is broken in some way. For example missing a }
or something like that.
这个特别的错误是V8的一个令人讨厌的事实。在大多数情况下,您的JavaScript会以某种方式被破坏。比如丢失一个}之类的东西。
Example given, this will yield "Unexpected end of input" too:
例如,这也将产生“意想不到的输入端”:
eval('[{"test": 4}') // notice the missing ]
But the root cause of the problems seems to be that the requested JSON url has a Content-Type of text/html
which Chrome apparently tries to parse as HTML, which then results in the unexpected end of input due to the fact that the included image tags are being parsed.
但问题的根源似乎在于,请求的JSON url有一个内容类型的文本/html, Chrome显然试图将其解析为html,然后由于包含的图像标签被解析而导致了输入的意外结束。
Try setting the Content-Type to text/plain
I think it should fix the issues.
尝试将内容类型设置为text/plain我认为它应该解决问题。
Nonetheless, V8 could do a better Job about telling one exactly where the input ended unexpectedly.
尽管如此,V8可以更好地告诉一个输入意外终止的确切位置。
#2
67
Try Firebug for Mozilla - it will show the position of the missing }
.
尝试为Mozilla的Firebug -它将显示丢失的}的位置。
http://getfirebug.com/
#3
28
See my case on another similar question:
请参见我关于另一个类似问题的案例:
In my case, I was trying to parse an empty JSON:
在我的例子中,我试图解析一个空JSON:
JSON.parse(stringifiedJSON);
In other words, what happened was the following:
换句话说,所发生的情况如下:
JSON.parse("");
#4
10
I get this error when I have ommitted a closing brace character (}
)in JavaScript code. Check that your braces are properly balanced.
当我省略了JavaScript代码中的闭括号字符(})时,就会出现这个错误。检查你的牙套是否平衡。
#5
9
For the record, for anyone trying to find the various causes for this error. An empty HTML5 data attribute
对于记录来说,对于任何试图找出这个错误的各种原因的人来说。一个空的HTML5数据属性
data-mydata = ''
causes the error too. You should check when the data value is a null string and not include the attribute at all. It goes without saying this is largely relevant to script generated HTML.
导致错误。您应该检查数据值何时为空字符串,而根本不包含属性。不用说,这很大程度上与脚本生成的HTML有关。
#6
8
The issue for me was that I was doing $.ajax with dataType: "json"
for a POST request that was returning an HTTP 201 (created) and no request body. The fix was to simply remove that key/value.
对我来说,问题是我在做$。使用dataType的ajax:“json”用于返回HTTP 201(创建)且没有请求主体的POST请求。解决方法是简单地删除该键/值。
#7
6
JSHint is good at finding the location of missing brackets or bad syntax.
JSHint擅长查找丢失的括号或错误语法的位置。
#8
5
Another cause of this error: if your API intentionally responds with no response body, but responds with a 200 OK
status code instead of a 204 No Content
status code. Some JavaScript libraries may not respond well to unexpected content types when there is no content, so use the correct status code!
这一错误的另一个原因是:如果您的API故意响应没有响应主体,而是使用200个OK状态码而不是204个no内容状态码。当没有内容时,一些JavaScript库可能不能很好地响应意外的内容类型,因此使用正确的状态代码!
#9
2
There will definitely be an open bracket which caused the error.
肯定会有一个左括号导致错误。
I'd suggest that you open the page in Firefox, then open Firebug and check the console – it'll show the missing symbol.
我建议您打开Firefox中的页面,然后打开Firebug并检查控制台——它将显示丢失的符号。
Example screenshot:
示例截图:
#10
0
I faced similar problem using ui bootstrap directive for angularjs - uib-datepicker, when pressing am/pm toggle.
当我按am/pm切换时,我遇到了类似的问题,使用angularjs - uibm -datepicker的ui引导指令。
Error in event handler for (unknown): SyntaxError: Unexpected end of JSON input angular timepicker
事件处理程序(未知)中的错误:SyntaxError: JSON输入角时间选择器的意外端
Turned out it was because of plugin 'Trans-over' (which translates a word when it is clicked). Maybe my answer will help someone, because I didn't found anything on the internet.
原来,这是因为插件“Trans-over”(当它被点击时,翻译为一个单词)。也许我的答案会对某些人有所帮助,因为我在网上找不到任何东西。
#11
0
Since it's an async operation the onreadystatechange
may happen before the value has loaded in the responseText, try using a window.setTimeout(function () { JSON.parse(xhr.responseText); }, 1000);
to see if the error persists? BOL
由于它是一个异步操作,所以onreadystatechange可能在该值加载到responseText之前发生,请尝试使用窗口。setTimeout(函数(){ JSON.parse(xhr.responseText);},1000);查看错误是否持续?波尔
#12
0
My problem was with Google Chrome cache. I tested this by running my web application on Firefox and I didn't got that error there. So then I decided trying emptying cache of Google Chrome and it worked.
我的问题是谷歌Chrome缓存。我通过在Firefox上运行我的web应用程序测试了这一点,我在那里没有发现错误。于是我决定试着清空谷歌Chrome的缓存,它成功了。