javascript D3.js:“未捕获的语法错误:意外的令牌非法”?

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

D3.js: "Uncaught SyntaxError: Unexpected token ILLEGAL"?

javascriptd3.js

提问by Richard

I've just downloaded D3.js from d3js.org(link to zip file), unzipped it, and referenced it in the following HTML page:

我刚刚从 d3js.org(链接到 zip 文件)下载了 D3.js,解压并在以下 HTML 页面中引用了它:

<html>
<head>
<title>D3 Sandbox</title>
<style>
</head>
<body>
<script src="/d3.v3.js"></script>
</body>
</html>

But when I load this page, my console (in Chrome) is giving me this error:

但是当我加载这个页面时,我的控制台(在 Chrome 中)给了我这个错误:

Uncaught SyntaxError: Unexpected token ILLEGAL: line 2

It doesn't like the pi and e symbols at the start of the file. Errrr... what can I do about this? I am serving the file with python's SimpleHTTPServer.

它不喜欢文件开头pi 和 e 符号。呃……我该怎么办?我正在使用 python 的 SimpleHTTPServer 提供文件。

Update: yes I know I can just link to a CDN version, but I would prefer to serve the file locally.

更新:是的,我知道我可以链接到 CDN 版本,但我更愿意在本地提供文件。

回答by Laurent Jégou

Try specifying the UTF-8 charset on the HTML host document :

尝试在 HTML 主机文档上指定 UTF-8 字符集:

<meta http-equiv="content-type" content="text/html; charset=UTF8">

<meta http-equiv="content-type" content="text/html; charset=UTF8">

D3 contains UTF-8 symbols (like π) invalids in non-UTF8 documents.

D3 在非 UTF8 文档中包含 UTF-8 符号(如 π)无效。

回答by T.J. Crowder

That sounds like a problem with encoding. I recommend The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!). Despite the somewhat condescending title, it contains some very useful information. Specifically, it sounds like your server is serving the d3.v3.jsfile with the wrong encoding.

这听起来像是编码问题。我推荐每个软件开发人员绝对、肯定必须了解 Unicode 和字符集的绝对最低要求(没有任何借口!)。尽管标题有些居高临下,但它包含一些非常有用的信息。具体来说,听起来您的服务器正在d3.v3.js以错误的编码提供文件。

回答by hewsonism

Add 'charset="utf-8"'

添加 'charset="utf-8"'

<script src="/d3.v3.js" charset="utf-8"></script>

回答by Nathan Bubna

I tried setting the charset in the doc and in the script tag itself, but Chrome doesn't seem to care. Not sure if i'm doing something wrong.

我尝试在文档和脚本标签本身中设置字符集,但 Chrome 似乎并不关心。不确定我是否做错了什么。

I did find success by running it through uglify with the --ascii-only option first.

我确实通过首先使用 --ascii-only 选项通过 uglify 运行它而取得了成功。

UPDATE: Turns out my View->Encoding setting in Chrome was not on Auto-Detect, but some Western encoding. Not sure why, but changing that resolved the problem. Ridiculous that that setting would trump a charset property right on the script tag. Considering that users might be in the same situation and not be able to figure it out, i'll still be using uglify to ensure success.

更新:原来我在 Chrome 中的 View->Encoding 设置不是自动检测,而是一些西方编码。不知道为什么,但改变它解决了问题。荒谬的是,该设置会胜过脚本标签上的字符集属性。考虑到用户可能处于相同的情况并且无法弄清楚,我仍然会使用 uglify 来确保成功。

回答by userFog

Check if you have a plus sign between all of your string concatenations, if you do not this error will be generated.

检查所有字符串连接之间是否有加号,如果没有,将生成此错误。