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

发布于 2024-09-16 17:48:07 字数 1321 浏览 8 评论 0 原文

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

Uncaught SyntaxError: Unexpected token <

对于这行代码:

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

在这种情况下:

$(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 });
                }
            }
        });
    });

我认为一切都应该很好,但我不明白为什么 < 不知何故把这个扔掉..

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

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

有什么想法吗?

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(12

孤蝉 2024-09-23 17:48:07

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

在设置 javascript 时尝试执行此操作:

<script>
//<![CDATA[

    // insert teh codez

//]]>
</script>

或者,将 javascript 移至单独的文件。

编辑:啊..通过该链接我已经找到了它。我说的问题根本就不是问题。 就是问题所在,从网站中删除:

<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 });
                }
            }
        });
    });

你能发现错误吗?它位于第一行: 标记未关闭。应该是

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

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

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

Try doing this when setting up your javascripts:

<script>
//<![CDATA[

    // insert teh codez

//]]>
</script>

Alternatively, move your javascript to a separate file.

Edit: Ahh.. with that link I've tracked it down. What I said was the issue wasn't the issue at all. this is 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 <script tag isn't closed. It should be
<script type="text/javascript">

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

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

通知家属抬走 2024-09-23 17:48:07

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

事实证明,当访问 /something/5 这样的 URL 时,我的 /index.html 实际上是在 /something/index.html(感谢我的.htaccess)。这会破坏我的 JavaScript 文件的所有相对 URL(来自 index.html 内部),这意味着它们应该对我进行 404 处理。

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

在我的例子中(因为我是从子目录内提供网站服务)的修复方法是在我的 html head 中添加一个基本标签。

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.

It turns out that when visiting a URL such as /something/5, my /index.html was 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.

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).

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.

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

堇年纸鸢 2024-09-23 17:48:07

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

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

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.

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.

高冷爸爸 2024-09-23 17:48:07

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

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

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.

⒈起吃苦の倖褔 2024-09-23 17:48:07

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

  1. 转到 IIS 管理器
  2. 打开站点
  3. 单击身份验证
  4. 编辑匿名身份验证。
  5. 选择“应用程序池标识”

在此处输入图像描述

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"

enter image description here

随波逐流 2024-09-23 17:48:07

我不知道这是否是一个迟到的答案,或者它会以某种方式帮助某人。

如果您下载JS文件并将其粘贴到项目目录中,则必须根据某些特定服务器的要求将它们放入目录中。例如,某些服务器会删除文件并将它们放在单个 BUILDDIST 文件夹中。您也可以将文件直接放在 index 文件所在的父文件夹中,问题就会消失。

I don't know if this will be a late answer, or it'll help somebody somehow.

If you download JS files and paste them into your project directories, you have to place them into directories according to the requirements of some specific servers. For example, some servers strip out files and place them on a single BUILD or DIST folder. You can as well place the files directly inside the parent folder where your index file is and the problem will disappear.

寻梦旅人 2024-09-23 17:48:07

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

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

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.

I had this problem and above solution worked for me.

鲸落 2024-09-23 17:48:07

我在使用带有 firebase 的网络应用程序时遇到此错误。这是由于在index.html中没有包含所需的库

为了在开发环境中解决这个问题,我在index.html中添加了以下代码

<script src="https://www.gstatic.com/firebasejs/8.1.1/firebase.js"></script>

行当您计划部署时,使用必要的库而不是导入所有js sdk用于生产。

I got this error while using the web app with firebase. It was due to not including the desired libraries in the index.html

To fix this in the development environment, I added following line of code in the index.html

<script src="https://www.gstatic.com/firebasejs/8.1.1/firebase.js"></script>

Use the necessary libraries instead of importing all the js sdk when you are planning to deploy for production.

森林散布 2024-09-23 17:48:07

我正在使用 MVC,将值从控制器传递到视图(Jquery 表),收到错误:

  • “jquery Uncaught SyntaxError: Unexpected token '&'”

花了几个小时,实际上,解决方案很简单:

var json = @Html.Raw(Model.Objectlist))

代码隐藏:

string Objectlist = Newtonsoft.Json.JsonConvert.SerializeObject(ViewModelObject);

下次,将节省我的时间。 如何从 Razor 模型对象获取 JSON 对象JavaScript

I am using MVC, pass value from controller to view (Jquery table), got the error:

  • "jquery Uncaught SyntaxError: Unexpected token '&'"

spent several hours, actually, solution is simple:

var json = @Html.Raw(Model.Objectlist))

Code behind:

string Objectlist = Newtonsoft.Json.JsonConvert.SerializeObject(ViewModelObject);

Next time, will save my time. How to get JSON object from Razor Model object in javascript

于我来说 2024-09-23 17:48:07

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

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"

依 靠 2024-09-23 17:48:07

尝试将 text/javascript 替换为 text/html,然后保存注释并重新加载浏览器,然后将其返回到 text/javascript。我不知道,但由于未知原因,这个对我有用。我正在搜索并复制-粘贴,我只是不小心撤消了我正在输入的内容,然后繁荣卡布卢伊它工作了0.O顺便说一句,我只是菜鸟..抱歉,我只是认为它可以帮助..

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..

瞄了个咪的 2024-09-23 17:48:07

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

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

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

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.

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

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