Chrome:未捕获的语法错误:输入意外结束
在 Google Chrome 中加载页面时,我在控制台中收到模糊错误:
未捕获的语法错误:输入意外结束
我不知道是什么原因造成的。我将如何调试这个错误?
When loading my page in Google Chrome, I get a vague error in the console:
Uncaught SyntaxError: Unexpected end of input
I have no idea what is causing it. How would I go about debugging this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
V8 中的错误消息“Unexpected end of input”通常表示 JavaScript 代码中存在语法错误,例如缺少
}
或类似内容。给出的示例,这也会产生“意外的输入结束”:
但问题的根本原因似乎是请求的 JSON url 的 Content-Type 为
text/html Chrome 显然会尝试将其解析为 HTML,这会导致输入意外结束,因为正在解析包含的图像标签。
尝试将 Content-Type 设置为
text/plain
我认为它应该可以解决问题。尽管如此,V8 可以做得更好,可以准确地告诉人们输入意外结束的何处。
The error message "Unexpected end of input" in V8 often indicates a syntax error in your JavaScript code, such as a missing
}
or similar.Example given, this will yield "Unexpected end of input" too:
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.Try setting the Content-Type to
text/plain
I think it should fix the issues.Nonetheless, V8 could do a better Job about telling one exactly where the input ended unexpectedly.
尝试 Mozilla 的 Firebug - 它会显示丢失的
}
的位置。http://getfirebug.com/
Try Firebug for Mozilla - it will show the position of the missing
}
.http://getfirebug.com/
请参阅另一个我的案例类似案例问题:
See my case on another similar question:
当我在 JavaScript 代码中省略右大括号字符 (
}
) 时,出现此错误。检查您的牙套是否适当平衡。
I get this error when I have ommitted a closing brace character (
}
)in JavaScript code.Check that your braces are properly balanced.
作为记录,供任何试图查找此错误的各种原因的人使用。空的 HTML5 数据属性
也会导致错误。您应该检查数据值何时为空字符串,并且根本不包含该属性。不用说,这很大程度上与脚本生成的 HTML 有关。
For the record, for anyone trying to find the various causes for this error. An empty HTML5 data attribute
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.
对我来说,问题是我正在使用
dataType: "json"
执行 $.ajax 来处理返回 HTTP 201(已创建)且没有请求正文的 POST 请求。修复方法是简单地删除该键/值。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.JSHint 擅长查找缺少括号或错误语法的位置。
JSHint is good at finding the location of missing brackets or bad syntax.
导致此错误的另一个原因是:如果您的 API 故意不响应响应正文,而是响应
200 OK
状态代码而不是204 No Content
状态代码。当没有内容时,某些 JavaScript 库可能无法很好地响应意外的内容类型,因此请使用正确的状态代码!Another cause of this error: if your API intentionally responds with no response body, but responds with a
200 OK
status code instead of a204 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!肯定会有一个开括号导致错误。
我建议您在 Firefox 中打开该页面,然后打开 Firebug 并检查控制台 - 它会显示丢失的符号。
屏幕截图示例:
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.
Example screenshot:
我的问题是 Google Chrome 缓存。我通过在 Firefox 上运行我的 Web 应用程序对此进行了测试,并且没有出现该错误。于是我决定尝试清空 Google Chrome 的缓存,结果成功了。
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.
如果您的 JavaScript 代码被缩小为一行,导致此错误的另一个原因是使用
//
而不是/**/
作为注释。不好(注释掉
//
之后的所有内容,包括函数的结束}
)好(限制您的评论)
In cases where your JavaScript code is minified to a single line, another cause for this error is using
//
instead of/**/
for your comments.Bad (comments out everything after
//
including the closing}
for your function)Good (confines your comment)
就我而言,我动态添加 JavaScript 并在字符串模板中使用双引号两次,因此我将第二个引号更改为单引号,并且错误消失了。
我希望它能帮助一些出于同样原因来到这里的人。
In my case I was adding javascript dynamicly and using double quotes 2 times in string templates so i changed the second to single quotes and the error was gone.
I hope it will help some of the people coming here for the same reason.
尝试解析空 JSON 可能是导致此错误的原因。
当您收到服务器或其他任何东西的响应时,首先检查它是否不为空。例如:
然后您可以使用以下方式获取:
Trying to parse an empty JSON can be the cause of this error.
When you receive a response from the server or whatever, check first if it's not empty. For example:
Then you can fetch with:
此错误的原因之一可能是网络基础设施。我在使用 Cloudflare 的 Web 代理服务时遇到了这个问题。一旦我禁用代理并清除浏览器缓存,它就开始工作。因此,还要检查网络组件的状态。就我而言,它是在 Cloudflare 状态页面 上传达的
One of the reasons for this error can be network infrastructure. I've got this problem by using a web proxy service from Cloudflare. As soon as I disable proxy and cleared browser cache it started working. So check the status of your network components too. In my case it was communicated at Cloudflare status page
当按下 am/pm 切换时,我遇到了类似的问题,使用 AngularJS - uib-datepicker 的 ui bootstrap 指令。
原来是因为插件 'Trans-over' (点击时翻译一个单词)。也许我的回答会对某人有所帮助,因为我在互联网上没有找到任何内容。
I faced similar problem using ui bootstrap directive for angularjs - uib-datepicker, when pressing am/pm toggle.
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.
由于这是一个异步操作,
onreadystatechange
可能会在值加载到responseText之前发生,请尝试使用window.setTimeout(function () { JSON.parse(xhr.responseText); }, 1000);
看看错误是否仍然存在?玻尔
Since it's an async operation the
onreadystatechange
may happen before the value has loaded in the responseText, try using awindow.setTimeout(function () { JSON.parse(xhr.responseText); }, 1000);
to see if the error persists? BOL
我遇到了这个错误,并通过在
readyState
和status
上添加防护来修复它,如下所示:I had this error and fixed it by adding the guard on
readyState
andstatus
shown here:如果锚标记出现错误,只需将“Onclick”替换为“href”或将“href”替换为“Onclick”
if you got error at Anchor tag,just replace "Onclick" with "href" or "href" with "Onclick"
当我遇到同样的问题时,将请求中的
Accept
标头设置为application/json
对我有用。Setting the
Accept
header toapplication/json
in the request worked for me when I faced the same problem.就我而言,我的网速很低,当我关闭其他用户的互联网连接时,错误就消失了,奇怪
In my case, i had low internet speed, when i turn off the other user's internet connection then error has gone, strange