Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

javascript - 我不断收到“ Uncaught SyntaxError:意外令牌o”(I keep getting “Uncaught SyntaxError: Unexpected token o”)

I'm trying to learn some html/css/javascript, so I'm writing myself a teaching project.(我正在尝试学习一些html / css / javascript,因此我正在为自己编写一个教学项目。)

The idea was to have some vocabulary contained in a json file which would then be loaded into a table.(想法是在json文件中包含一些词汇,然后将其加载到表中。)

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:(我的JavaScript代码包含在一个单独的文件中,就是这样:)

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:(我的JSON文件现在具有以下内容:)

[
    {
        "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);(现在,该错误在第11行中报告,即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).(当我删除json文件时,出现错误:“ GET http://.../wokab.json 404(未找到)”,因此我知道它正在加载(或至少尝试加载)。)

  ask by Bjorninn translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like jQuery takes a guess about the datatype.(看起来jQuery猜测了数据类型。)

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.(即使您没有调用getJSON(),它也会进行JSON解析-然后,当您尝试在对象上调用JSON.parse()时,就会收到错误消息。)

Further explanation can be found in Aditya Mittal's answer .(进一步的解释可以在Aditya Mittal的答案中找到 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...