jQuery 错误:未捕获的语法错误:意外的标记 <

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3630006/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:47:17  来源:igfitidea点击:

Error: Uncaught SyntaxError: Unexpected token <

jquerytoken

提问by Qcom

For some reason, I'm getting this error message:

出于某种原因,我收到此错误消息:

Uncaught SyntaxError: Unexpected token <

For this line of code:

对于这行代码:

title: '<img src="/images/text/text_mario_planet_jukebox.png" id="text_mario_planet_jukebox"/>',

In this context:

在这种情况下:

$(document).ready(function() {
    $('#infobutton').click(function() {
        $('#music_descrip').dialog('open');
    });
        $('#music_descrip').dialog({
            title: '<img src="/images/text/text_mario_planet_jukebox.png" id="text_mario_planet_jukebox"/>',
            autoOpen: false,
            height: 375,
            width: 500,
            modal: true,
            resizable: false,
            buttons: {
                'Without Music': function() {
                    $(this).dialog('close');
                    $.cookie('autoPlay', 'no', { expires: 365 * 10 });
                },
                'With Music': function() {
                    $(this).dialog('close');
                    $.cookie('autoPlay', 'yes', { expires: 365 * 10 });
                }
            }
        });
    });

I think everything should be good to go, but I don't understand why the <is somehow throwing this off..

我认为一切都应该很好,但我不明白为什么<要以某种方式抛弃它..

whoops, forgot to show where this is happening! My bad,

哎呀,忘了说明这是在哪里发生的!我的错,

http://www.marioplanet.com/index.asp

http://www.marioplanet.com/index.asp

Any ideas?

有任何想法吗?

采纳答案by Randolpho

This is a browser issue rather than a javascript or JQuery issue; it's attempting to interpret the angle bracket as an HTML tag.

这是浏览器问题,而不是 javascript 或 JQuery 问题;它试图将尖括号解释为 HTML 标记。

Try doing this when setting up your javascripts:

在设置 javascripts 时尝试这样做:

<script>
//<![CDATA[

    // insert teh codez

//]]>
</script>

Alternatively, move your javascript to a separate file.

或者,将您的 javascript 移动到一个单独的文件。

Edit: Ahh.. with that link I've tracked it down. What I said was the issue wasn't the issue at all. thisis the issue, stripped from the website:

编辑:啊..我已经找到了那个链接。我说的是问题根本不是问题。是从网站上剥离的问题:

<script type="text/javascript"
    $(document).ready(function() {
    $('#infobutton').click(function() {
        $('#music_descrip').dialog('open');
    });
        $('#music_descrip').dialog({
            title: '<img src="/images/text/text_mario_planet_jukebox.png" id="text_mario_planet_jukebox"/>',
            autoOpen: false,
            height: 375,
            width: 500,
            modal: true,
            resizable: false,
            buttons: {
                'Without Music': function() {
                    $(this).dialog('close');
                    $.cookie('autoPlay', 'no', { expires: 365 * 10 });
                },
                'With Music': function() {
                    $(this).dialog('close');
                    $.cookie('autoPlay', 'yes', { expires: 365 * 10 });
                }
            }
        });
    });

Can you spot the error? It's in the first line: the <scripttag isn't closed. It should be <script type="text/javascript">

你能发现错误吗?它在第一行:<script标签没有关闭。它应该是 <script type="text/javascript">

My previous suggestion still stands, however: you shouldenclose intra-tagged scripts in a CDATA block, or move them to a separately linked file.

但是,我之前的建议仍然有效:您应该将内部标记脚本包含在 CDATA 块中,或者将它们移动到单独链接的文件中。

That wasn't the issue here, but it would have shown the real issue faster.

这不是这里的问题,但它会更快地显示真正的问题。

回答by jeff-h

I too got this error, when developing a Backbone application using HTML5 push state in conjunction with an .htaccess which redirects any unknown files to index.html.

我也遇到了这个错误,在开发一个使用 HTML5 推送状态的 Backbone 应用程序时,结合 .htaccess 将任何未知文件重定向到 index.html。

It turns out that when visiting a URL such as /something/5, my /index.htmlwas effectively being served at /something/index.html(thanks to my .htaccess). This had the effect of breaking all the relative URLs to my JavaScript files (from inside the index.html ), which meant that they should have 404'd on me.

事实证明,当访问诸如 之类的 URL 时/something/5,my/index.html有效地被服务于/something/index.html(感谢我的 .htaccess)。这会破坏我的 JavaScript 文件(从 index.html 内部)的所有相对 URL,这意味着它们应该对我进行 404 处理。

However, again due to my htaccess, instead of 404'ing when attempting to retrieve the JS files, they instead returned my index.html. Thus the browser was given an index.html for every JavaScript file it tried to pull in, and when it evaluated the HTML as if it were JavaScript, it returned a JS error due to the leading < (from my tag in index.html).

然而,再次由于我的 htaccess,而不是 404'ing 在尝试检索 JS 文件时,他们反而返回了我的 index.html。因此,浏览器为它尝试引入的每个 JavaScript 文件提供了一个 index.html,并且当它像评估 JavaScript 一样评估 HTML 时,由于前导 <(来自我在 index.html 中的标记),它返回了一个 JS 错误.

The fix in my case (as I was serving the site from inside a subdirectory) was to add a base tag in my html head.

在我的情况下(因为我从子目录内为站点提供服务)的修复是在我的 html 头中添加一个基本标记。

<base href="/my-app/">

<base href="/my-app/">

回答by user738048

I had the exact same symptom, and this was my problem, very tricky to track down, so I hope it helps someone.

我有完全相同的症状,这是我的问题,很难追踪,所以我希望它可以帮助某人。

I was using JQuery parseJSON()and the content I was attempting to parse was actually not JSON, but an error page that was being returned.

我正在使用JQuery parseJSON()并且我试图解析的内容实际上不是 JSON,而是正在返回的错误页面。

回答by david.barkhuizen

Typically this happens when you are trying to load a non-html resource (e.g the jquery library script file as type text/javascript) and the server, instead of returning the expected JS resource, returns HTML instead - typically a 404 page.

通常,当您尝试加载非 html 资源(例如,类型为 text/javascript 的 jquery 库脚本文件)并且服务器不返回预期的 JS 资源,而是返回 HTML 时,通常会发生这种情况 - 通常是 404 页面。

The browser then attempts to parse the resource as JS, but since what was actually returned was an HTML page, the parsing fails with a syntax error, and since an HTML page will typically start with a < character, this is the character that is highlighted in the syntax error exception.

浏览器然后尝试将资源解析为 JS,但由于实际返回的是一个 HTML 页面,解析失败并出现语法错误,并且由于 HTML 页面通常以 < 字符开头,这是突出显示的字符在语法错误异常中。

回答by zui

After trying several solutions, this worked for me:

在尝试了几种解决方案后,这对我有用:

  1. Go to IIS Manager
  2. Open the Site
  3. Click on Authentication
  4. Edit Anonymous Authentication.
  5. Select "Application pool identity"
  1. 进入 IIS 管理器
  2. 打开网站
  3. 点击认证
  4. 编辑匿名身份验证。
  5. 选择“应用程序池标识”

example

例子

回答by M. Stefanczuk

If you face this issue only in Chrome and in IE, for instance, everything is fine, then just enable checkbutton "Disable cache" in Dev Tools (F12 -> "Network" tab) and clear history in the last hour.

例如,如果您仅在 Chrome 和 IE 中遇到此问题,那么一切都很好,那么只需在开发工具(F12 ->“网络”选项卡)中启用检查按钮“禁用缓存”并清除过去一小时的历史记录。

I had this problem and above solution worked for me.

我遇到了这个问题,上述解决方案对我有用。

回答by qw12

try to replace the text/javascript to text/html then save the note and reload browser then bring it back to text/javascript.. I don't know but for unknown reason this one works with me.. I am searching and copy-pasting and I just accidentally undo what I am typing then boom kablooey it worked 0.O by the way I am just noob.. sorry I just thought it could help though..

尝试将 text/javascript 替换为 text/html,然后保存笔记并重新加载浏览器,然后将其带回 text/javascript .. 我不知道,但由于未知原因,这对我有用.. 我正在搜索和复制-粘贴,我只是不小心撤消了我正在输入的内容然后繁荣 kablooey 它工作 0.O 顺便说一下我只是菜鸟..对不起,我只是认为它可以帮助..

回答by Greg

It usually works when you change the directory name, where the file is. It worked for me when I moved it from "js/" to "../js"

当您更改文件所在的目录名称时,它通常会起作用。当我将它从“js/”移动到“../js”时它对我有用

回答by holmescn

I don't know whether you got the answer. I met this issue today, and I thought I got a possible right answer: you don't put the script file in a right location.

不知道你有没有得到答案。我今天遇到了这个问题,我想我得到了一个可能的正确答案:您没有将脚本文件放在正确的位置。

Most people don't meet this issue because they put the scripts into their server directory directly. I meet this because I make a link of the source file (the html file) to the server root directory. I didn't link the script files, and I don't know how nginx find them. But they are not loaded correctly.

大多数人不会遇到这个问题,因为他们直接将脚本放入他们的服务器目录中。我遇到这个是因为我将源文件(html 文件)链接到服务器根目录。我没有链接脚本文件,我不知道nginx是如何找到它们的。但是它们没有正确加载。

After I linked all files to the server root directory, problem solved.

将所有文件链接到服务器根目录后,问题解决了。