Chrome:未捕获的语法错误:输入意外结束

发布于 2024-09-16 03:32:59 字数 129 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

骷髅 2024-09-23 03:33:00

V8 中的错误消息“Unexpected end of input”通常表示 JavaScript 代码中存在语法错误,例如缺少 } 或类似内容。

给出的示例,这也会产生“意外的输入结束”:

eval('[{"test": 4}') // notice the missing ]

但问题的根本原因似乎是请求的 JSON url 的 Content-Typetext/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:

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.

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.

想念有你 2024-09-23 03:33:00

尝试 Mozilla 的 Firebug - 它会显示丢失的 } 的位置。

http://getfirebug.com/

Try Firebug for Mozilla - it will show the position of the missing }.

http://getfirebug.com/

镜花水月 2024-09-23 03:33:00

请参阅另一个我的案例类似案例问题

就我而言,我试图解析一个空的 JSON:

JSON.parse(stringifiedJSON);

换句话说,发生的事情如下:

JSON.parse("");

See my case on another similar question:

In my case, I was trying to parse an empty JSON:

JSON.parse(stringifiedJSON);

In other words, what happened was the following:

JSON.parse("");
北方的韩爷 2024-09-23 03:33:00

当我在 JavaScript 代码中省略右大括号字符 (}) 时,出现此错误。
检查您的牙套是否适当平衡。

I get this error when I have ommitted a closing brace character (})in JavaScript code.
Check that your braces are properly balanced.

放血 2024-09-23 03:33:00

作为记录,供任何试图查找此错误的各种原因的人使用。空的 HTML5 数据属性

data-mydata = ''

也会导致错误。您应该检查数据值何时为空字符串,并且根本不包含该属性。不用说,这很大程度上与脚本生成的 HTML 有关。

For the record, for anyone trying to find the various causes for this error. An empty HTML5 data attribute

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.

稍尽春風 2024-09-23 03:33:00

对我来说,问题是我正在使用 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.

や三分注定 2024-09-23 03:33:00

JSHint 擅长查找缺少括号或错误语法的位置。

JSHint is good at finding the location of missing brackets or bad syntax.

看海 2024-09-23 03:33:00

导致此错误的另一个原因是:如果您的 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 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!

嘿看小鸭子会跑 2024-09-23 03:33:00

肯定会有一个开括号导致错误。

我建议您在 Firefox 中打开该页面,然后打开 Firebug 并检查控制台 - 它会显示丢失的符号。

屏幕截图示例:

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:

Firebug highlighting the error

柠檬色的秋千 2024-09-23 03:33:00

我的问题是 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.

苍风燃霜 2024-09-23 03:33:00

如果您的 JavaScript 代码被缩小为一行,导致此错误的另一个原因是使用 // 而不是 /**/ 作为注释。

不好(注释掉 // 之后的所有内容,包括函数的结束 }

function example() { //comment console.log('TEST'); }

好(限制您的评论)

function example() { /* comment */ console.log('TEST'); }

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)

function example() { //comment console.log('TEST'); }

Good (confines your comment)

function example() { /* comment */ console.log('TEST'); }
怎会甘心 2024-09-23 03:33:00

就我而言,我动态添加 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.

但可醉心 2024-09-23 03:33:00

尝试解析空 JSON 可能是导致此错误的原因。

当您收到服务器或其他任何东西的响应时,首先检查它是否不为空。例如:

function parseJSON(response) {
    if (response.status === 204 || response.status === 205) {
        return null;
    }
    return response.json();
}

然后您可以使用以下方式获取:

fetch(url, options).then(parseJSON); 

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:

function parseJSON(response) {
    if (response.status === 204 || response.status === 205) {
        return null;
    }
    return response.json();
}

Then you can fetch with:

fetch(url, options).then(parseJSON); 
海风掠过北极光 2024-09-23 03:33:00

此错误的原因之一可能是网络基础设施。我在使用 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
enter image description here

冰雪之触 2024-09-23 03:33:00

当按下 am/pm 切换时,我遇到了类似的问题,使用 AngularJS - uib-datepicker 的 ui bootstrap 指令。

事件处理程序中出现错误(未知):SyntaxError:意外结束
JSON 输入角度时间选择器

原来是因为插件 'Trans-over' (点击时翻译一个单词)。也许我的回答会对某人有所帮助,因为我在互联网上没有找到任何内容。

I faced similar problem using ui bootstrap directive for angularjs - uib-datepicker, when pressing am/pm toggle.

Error in event handler for (unknown): SyntaxError: Unexpected end of
JSON input angular timepicker

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.

如歌彻婉言 2024-09-23 03:33:00

由于这是一个异步操作,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 a window.setTimeout(function () { JSON.parse(xhr.responseText); }, 1000);
to see if the error persists? BOL

触ぅ动初心 2024-09-23 03:33:00

我遇到了这个错误,并通过在 readyStatestatus 上添加防护来修复它,如下所示:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
          // Your code here
   }
};

I had this error and fixed it by adding the guard on readyState and status shown here:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
          // Your code here
   }
};
红焚 2024-09-23 03:33:00

如果锚标记出现错误,只需将“Onclick”替换为“href”或将“href”替换为“Onclick”

if you got error at Anchor tag,just replace "Onclick" with "href" or "href" with "Onclick"

一萌ing 2024-09-23 03:33:00

当我遇到同样的问题时,将请求中的 Accept 标头设置为 application/json 对我有用。

Setting the Accept header to application/json in the request worked for me when I faced the same problem.

蝶…霜飞 2024-09-23 03:33:00

就我而言,我的网速很低,当我关闭其他用户的互联网连接时,错误就消失了,奇怪

In my case, i had low internet speed, when i turn off the other user's internet connection then error has gone, strange

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文