Javascript 未捕获的语法错误:意外的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26102741/
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 number
提问by Burak Karaku?
SOLUTION: I realized that the current Regional Setting of test environment is set to Turkish, and it uses comma for decimal symbol. In my local, it is set to UK, and that's the reason that the code works in my local and doesn't work in test. I guess I'll replace all commas with dots beforehand. Thanks for all the replies.
解决方案:我意识到测试环境的当前区域设置设置为土耳其语,并且它使用逗号作为十进制符号。在我的本地,它设置为英国,这就是代码在我的本地工作而在测试中不起作用的原因。我想我会事先用点替换所有逗号。感谢所有的答复。
I'm trying to fill a bar chart with following data:
我正在尝试用以下数据填充条形图:
var oneToTen = [0,1,2,3,4,5,6,7,8,9,10];
var ticks = [[0, 'Atmosfer'],[1, 'Servis'],[2, 'Yemeklerimiz']];
var labels = ['Atmosfer','Servis','Yemeklerimiz'];
var mainQuest_d1 = 8,16666666666667;
var mainQuest_d2 = 7,95833333333333;
var mainQuest_d3 = 8,125;
var d_main_quest_bar = [[0, 8,16666666666667],[1, 7,95833333333333],[2, 8,125]];
I get this error:
我收到此错误:
Uncaught SyntaxError: Unexpected number
未捕获的语法错误:意外的数字
I can't see what's wrong the code above. It works fine in localhost, but when I publish it to the test server, it gives this error.
我看不出上面的代码有什么问题。它在 localhost 中工作正常,但是当我将其发布到测试服务器时,它会出现此错误。
Complete code that's not yet rendered by Razor:
Razor 尚未呈现的完整代码:
int i = 0;
int j = 0;
int m = 0;
@Html.Raw("var oneToTen = [0,1,2,3,4,5,6,7,8,9,10];");
@Html.Raw("var ticks = [");
if (Model.MainQuestionsRatingList != null)
{
foreach (var item in Model.MainQuestionsRatingList)
{
j++;
@Html.Raw("["+(j-1)+", '"+item.QuestionText+"']")
if (j != Model.MainQuestionsRatingList.Count) { @Html.Raw(","); }
}
}
@Html.Raw("];");
@Html.Raw("var labels = [");
if (Model.MainQuestionsRatingList != null)
{
foreach (var item in Model.MainQuestionsRatingList)
{
m++;
@Html.Raw("'"+item.QuestionText+"'")
if (m != Model.MainQuestionsRatingList.Count) { @Html.Raw(","); }
}
}
@Html.Raw("];");
if (Model.MainQuestionsRatingList != null)
{
foreach (var item in Model.MainQuestionsRatingList)
{
i++;
@Html.Raw("var mainQuest_d" + i + " = " + item.Avg + ";");
}
}
i = 0;
@Html.Raw("var d_main_quest_bar = [");
if (Model.MainQuestionsRatingList != null)
{
foreach (var item in Model.MainQuestionsRatingList)
{
i++;
@Html.Raw("[" + (i-1) + ", "+item.Avg+"]");
if (i != Model.MainQuestionsRatingList.Count) { @Html.Raw(","); }
}
}
@Html.Raw("];");
}
data.push({
label: labels,
data: d_main_quest_bar,
bars: {
show: true,
barWidth: 0.2,
order: 1
}
});
EDIT: I ran the same code in my local, and figured out that the commas are automatically replaced with dots and that's why it works in my local as @T.J. Crowder said. But it doesn't happen when I run it in test. How is that possible?
编辑:我在我的本地运行了相同的代码,并发现逗号会自动替换为点,这就是为什么它在我的本地工作,正如@TJ Crowder 所说。但是当我在测试中运行它时它不会发生。这怎么可能?
回答by Matt Ball
You can't use localized decimal separator characters in JavaScript source code. You must use .:
您不能在 JavaScript 源代码中使用本地化的十进制分隔符。您必须使用.:
var mainQuest_d1 = 8.16666666666667;
var mainQuest_d2 = 7.95833333333333;
var mainQuest_d3 = 8.125;
See What is the decimal separator symbol in JavaScript?
It should be obvious that ,has another meaning already. How many elements do you expect the array
很明显,,这已经有另一个含义了。你期望数组有多少个元素
[0, 8,16666666666667]
to contain?
包含装有?
回答by musefan
You shouldn't use commas in your numbers. Use a decimal place instead. Commas are special characters reserved for other uses, such as separators in arrays and function parameters.
您不应该在数字中使用逗号。请改用小数位。逗号是保留用于其他用途的特殊字符,例如数组和函数参数中的分隔符。
For example:
例如:
8,16666666666667
should be
应该
8.16666666666667
You have a few instances so here is the full code written correctly:
您有几个实例,所以这里是正确编写的完整代码:
var oneToTen = [0,1,2,3,4,5,6,7,8,9,10];
var ticks = [[0, 'Atmosfer'],[1, 'Servis'],[2, 'Yemeklerimiz']];
var labels = ['Atmosfer','Servis','Yemeklerimiz'];
var mainQuest_d1 = 8.16666666666667;
var mainQuest_d2 = 7.95833333333333;
var mainQuest_d3 = 8.125;
var d_main_quest_bar = [[0, 8.16666666666667],[1, 7.95833333333333],[2, 8.125]];
(there are 6 changes in total across the last 4 lines)
(最后 4 行总共有 6 个变化)
回答by Wissam El-Kik
You shouldn't use commas for integers:
你不应该对整数使用逗号:
<script type="text/javascript">
var oneToTen = [0,1,2,3,4,5,6,7,8,9,10];
var ticks = [[0, 'Atmosfer'],[1, 'Servis'],[2, 'Yemeklerimiz']];
var labels = ['Atmosfer','Servis','Yemeklerimiz'];
var mainQuest_d1 = 8.16666666666667;
var mainQuest_d2 = 7.95833333333333;
var mainQuest_d3 = 8.125;
var d_main_quest_bar = [[0, 8.16666666666667],[1, 7.95833333333333],[2, 8.125]];
</script>
Source: http://en.wikipedia.org/wiki/JavaScript_syntax#Number
回答by Brian
This is incorrect:
这是不正确的:
var mainQuest_d1 = 8,16666666666667;
var mainQuest_d2 = 7,95833333333333;
var mainQuest_d3 = 8,125;
You can't use commas there.
你不能在那里使用逗号。

