javascript 未捕获的语法错误:意外的令牌 =
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14080401/
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
Uncaught SyntaxError: Unexpected token =
提问by Wex
I just switched from using a local copy of the minified version of d3.v3 to the development version. It worked fine when using the minified version, but using my local copy of http://d3js.org/d3.v3.jsgives me the error in the title, referencing this line:
我刚刚从使用 d3.v3 的缩小版本的本地副本切换到开发版本。使用缩小版本时它工作正常,但使用我的http://d3js.org/d3.v3.js 的本地副本给了我标题中的错误,引用了这一行:
var = Math.PI, μ = 1e-6, d3_radians = / 180, d3_degrees = 180 / ;
When I include the hosted file, it works fine.
当我包含托管文件时,它工作正常。
回答by mbostock
The problem is that you are serving D3 with the ISO-8859-1 character encoding (often the browser default), whereas D3 must be served with UTF-8 encoding. Typically this happens because you are missing a meta tag at the top of the loading HTML page:
问题是您使用 ISO-8859-1 字符编码(通常是浏览器默认值)为 D3 提供服务,而 D3 必须使用 UTF-8 编码提供。通常发生这种情况是因为您在加载 HTML 页面的顶部缺少元标记:
<!DOCTYPE html>
<meta charset="utf-8">
The meta-specified charset is required because d3js.orgis served by GitHub Pagesand does not specify a charset in the Content-Type response header. The charset is therefore inferred from the loading HTML document.
元指定字符集是必需的,因为d3js.org由GitHub Pages 提供服务,并且未在 Content-Type 响应标头中指定字符集。因此,字符集是从加载的 HTML 文档中推断出来的。
If you prefer, you can specify a charset attribute on the script tag. Make sure you clear your browser cache before testing, as the cached copy will retain the character encoding from when it was originally accessed:
如果您愿意,可以在脚本标记上指定字符集属性。确保在测试前清除浏览器缓存,因为缓存副本将保留最初访问时的字符编码:
<script src="http://d3js.org/d3.v3.js" charset="utf-8"></script>
The error does not occur with the minified version because the variable names are replaced with ASCII equivalents. (I don't recall offhand if UTF-8 characters in format strings are likewise replaced with escape sequences, but I still recommend serving D3 as UTF-8 in all cases.)
缩小版本不会发生该错误,因为变量名称已替换为 ASCII 等效项。(我不记得格式字符串中的 UTF-8 字符是否同样被转义序列替换,但我仍然建议在所有情况下将 D3 用作 UTF-8。)
Encoding problems can also happen if you downloaded D3 by viewing the source in your browser and then using copy-paste, which is why I recommend downloading d3.v3.zip.
如果您通过在浏览器中查看源代码然后使用复制粘贴来下载 D3,也会发生编码问题,这就是我建议下载d3.v3.zip 的原因。
回答by Francesco
it is definitely an encoding issue, try to focus on that.
这绝对是一个编码问题,尽量专注于此。
Opening with Chrome the link you posted (http://d3js.org/d3.v3.js) i see that double-byte characters too:
使用 Chrome 打开您发布的链接 ( http://d3js.org/d3.v3.js) 我也看到双字节字符:
var ? = Math.PI, ?μ = 1e-6, d3_radians = ? / 180, d3_degrees = 180 / ?;
if you barely save the file with "save page as..." and you open it with an editor like Sublime Text (http://www.sublimetext.com/) it works fine and it shows:
如果您几乎不使用“将页面另存为...”来保存文件,然后使用 Sublime Text ( http://www.sublimetext.com/) 之类的编辑器打开它,它就可以正常工作并显示:
var π = Math.PI, ε = 1e-6, d3_radians = π / 180, d3_degrees = 180 / π;
i tried to use this downloaded file with a projects of mine and it's all right.
我尝试将此下载的文件与我的项目一起使用,这没问题。