简体   繁体   English

未捕获的SyntaxError:意外的令牌=

[英]Uncaught SyntaxError: Unexpected token =

I just switched from using a local copy of the minified version of d3.v3 to the development version. 我刚刚使用缩小版d3.v3的本地副本切换到开发版本。 It worked fine when using the minified version, but using my local copy of http://d3js.org/d3.v3.js gives me the error in the title, referencing this line: 它在使用缩小版本时工作正常,但使用我的本地副本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. 当我包含托管文件时,它工作正常。

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. 问题是您使用ISO-8859-1字符编码(通常是浏览器默认值)为D3服务,而D3必须使用UTF-8编码。 Typically this happens because you are missing a meta tag at the top of the loading HTML page: 通常会发生这种情况,因为您在加载HTML页面的顶部缺少元标记:

<!DOCTYPE html>
<meta charset="utf-8">

The meta-specified charset is required because d3js.org is served by GitHub Pages and does not specify a charset in the Content-Type response header. 元指定的字符集是必需的,因为d3js.orgGitHub Pages提供 ,并且未在Content-Type响应头中指定字符集。 The charset is therefore inferred from the loading HTML document. 因此,从加载HTML文档推断出字符集。

If you prefer, you can specify a charset attribute on the script tag. 如果您愿意,可以在脚本标记上指定charset属性。 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. 缩小版本不会发生错误,因为变量名称将替换为ASCII等效项。 (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.) (我不记得格式字符串中的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的原因

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. 我试图将这个下载的文件与我的项目一起使用,这没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM