我不断收到“Uncaught SyntaxError: Unexpected token o" [英] I keep getting "Uncaught SyntaxError: Unexpected token o"
问题描述
我正在尝试学习一些 html/css/javascript,所以我正在为自己编写一个教学项目.
这个想法是在 json 文件中包含一些词汇,然后将其加载到表格中.我设法加载文件并打印出其中的一个值,然后我开始编写代码将这些值加载到表中.
这样做之后,我开始遇到错误,所以我删除了我编写的所有代码,只留下一行(以前工作过的同一行)......只是错误仍然存在.
错误如下:
Uncaught SyntaxError: Unexpected token o(匿名函数)script.js:10jQuery.Callbacks.firejquery-1.7.js:1064jQuery.Callbacks.self.fireWithjquery-1.7.js:1182donejquery-1.7.js:7454jQuery.ajaxTransport.send.callback
我的 javascript 代码包含在一个单独的文件中,就是这样:
function loadPageIntoDiv(){document.getElementById("wokabWeeks").style.display = "block";}函数 loadWokab(){//也试过getJSON,它抛出了同样的错误jQuery.get('wokab.json', 函数(数据) {var Glacier = JSON.parse(data);});}
我的 JSON 文件现在只有以下内容:
<预><代码>[{"中文": "包","假名": "卡班",汉字":K"},{"中文": "眼镜","假名": "梅甘娜",汉字":M"}]现在错误报告在第 11 行,即 var Glacier = JSON.parse(data);
行.
当我删除 json 文件时出现错误:GET http://.../wokab.json 404(未找到)"所以我知道它正在加载它(或至少尝试加载).
看起来 jQuery 需要猜测数据类型.即使您没有调用 getJSON(),它也会进行 JSON 解析——然后当您尝试对对象调用 JSON.parse() 时,您会收到错误消息.
在Aditya Mittal 的回答中可以找到进一步的解释.
I'm trying to learn some html/css/javascript, so I'm writing myself a teaching project.
The idea was to have some vocabulary contained in a json file which would then be loaded into a table. I managed to load the file in and print out one of its values, after which I began writing the code to load the values into the table.
After doing that I started getting an error, so I removed all the code I had written, leaving me with only one line (the same line that had worked before) ... only the error is still there.
The error is as follows:
Uncaught SyntaxError: Unexpected token o
(anonymous function)script.js:10
jQuery.Callbacks.firejquery-1.7.js:1064
jQuery.Callbacks.self.fireWithjquery-1.7.js:1182
donejquery-1.7.js:7454
jQuery.ajaxTransport.send.callback
My javascript code is contained in a separate file and is simply this:
function loadPageIntoDiv(){
document.getElementById("wokabWeeks").style.display = "block";
}
function loadWokab(){
//also tried getJSON which threw the same error
jQuery.get('wokab.json', function(data) {
var glacier = JSON.parse(data);
});
}
And my JSON file just has the following right now:
[
{
"english": "bag",
"kana": "kaban",
"kanji": "K"
},
{
"english": "glasses",
"kana": "megane",
"kanji": "M"
}
]
Now the error is reported in line 11 which is the var glacier = JSON.parse(data);
line.
When I remove the json file I get the error: "GET http://.../wokab.json 404 (Not Found)" so I know it's loading it (or at least trying to).
Looks like jQuery takes a guess about the datatype. It does the JSON parsing even though you're not calling getJSON()-- then when you try to call JSON.parse() on an object, you're getting the error.
Further explanation can be found in Aditya Mittal's answer.
这篇关于我不断收到“Uncaught SyntaxError: Unexpected token o"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!