我第一次使用json w / jquery并需要检索json文件。即使使用此脚本,我也无法检索json文件。我从未到过警告线("我在成功功能"); 。这是我使用的脚本:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- jQuery CDN -->
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/readable/bootstrap.min.css">
<title>Quiz App</title>
</head>
<script>
$(document).ready(function() {
alert("I'm in doc_ready");
// jQuery AJAX request to read JSON formatted Test Questions & Answers from file
$.ajax({
url: "test.json",
dataType: "json",
success: function(result) {
alert("I'm in success function");
alert(result);
}
});
});
</script>
<h3>Test for fetching JSON file from server</h3>
当我在ajax调用中放置断点时,我打印出第一个警报。但成功的那个没有。我看到它从萤火虫中踩到它完全跳过成功选项。我为什么不知所措。抛出没有异常,我从http响应主体看到:
[{
question: ["Who is Prime Minister of the United Kingdom?", "Who is the Vice-President of the United
States?", "Who is Chancellor of Germany?", "Who is the Prime Minister of Canada?"],
choices: [
["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
["Barack Obama", "Bernie Sanders", "Nancy Pelosi", "Joe Biden"],
["Franz Ritz", "Angela Merkel", "Jan Schroeder", "John Stevens", "Karl Marx"],
["Wayne Gretsky", "Pierre Trudeau", "Mike Myers", "Justin Trudeau", "Justin Bieber"]
],
correctAnswer: [0, 3, 1, 3]
}]
服务器正在完成它的工作。任何输入都非常赞赏。
更新:现在我将[对象对象]置于警报(结果),因为我将json文件放到JSON Generator站点。
当我把我的json通过JSON生成器时,我得到了这个在JSLint上验证正确:
[{
"question": [
"Who is Prime Minister of the United Kingdom?",
"Who is the Vice-President of the United States?",
"Who is Chancellor of Germany?",
"Who is the Prime Minister of Canada?"
],
"choices": [
[
"David Cameron",
"Gordon Brown",
"Winston Churchill",
"Tony Blair"
],
[
"Barack Obama",
"Bernie Sanders",
"Nancy Pelosi",
"Joe Biden"
],
[
"Franz Ritz",
"Angela Merkel",
"Jan Schroeder",
"John Stevens",
"Karl Marx"
],
[
"Wayne Gretsky",
"Pierre Trudeau",
"Mike Myers",
"Justin Trudeau",
"Justin Bieber"
]
],
"correctAnswer": [
0,
3,
1,
3
]
}]
更新II:一旦json格式问题得到纠正,这是jQuery代码
var allQuestions = null;
// jQuery AJAX request to read JSON formatted Test Questions & Answers from file
$.ajax({
url: "test.json",
dataType: "json",
success: function(result) {
allQuestions = result;
}
})
真的&#34;少写,做多&#34;是对的。
答案 0 :(得分:1)
您的数据不是JSON。必须引用属性名称。使用complete
处理程序,无论调用的结果是什么,都将调用它。
$.ajax({
url: "test.json",
dataType: "json",
complete: function(jqXhr, status) {
// Status will be "parsererror"
alert("I'm in complete function: " + status);
}
});
您的JSON应如下所示。请注意"question"
,"choices"
和"correctAnswer"
[{
"question": ["Who is Prime Minister of the United Kingdom?", "Who is the Vice-President of the United
States?", "Who is Chancellor of Germany?", "Who is the Prime Minister of Canada?"],
"choices": [
["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
["Barack Obama", "Bernie Sanders", "Nancy Pelosi", "Joe Biden"],
["Franz Ritz", "Angela Merkel", "Jan Schroeder", "John Stevens", "Karl Marx"],
["Wayne Gretsky", "Pierre Trudeau", "Mike Myers", "Justin Trudeau", "Justin Bieber"]
],
"correctAnswer": [0, 3, 1, 3]
}]
答案 1 :(得分:0)
你的JSON中实际上有一个无效字符,这个字符一直是问题数组中有一个断行。
在这里查看代码的工作示例,您只需要将JSON看作我的数据变量。
var data = [{
question: ["Who is Prime Minister of the United Kingdom?", "Who is the Vice-President of the United States?", "Who is Chancellor of Germany?", "Who is the Prime Minister of Canada?"],
choices: [
["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
["Barack Obama", "Bernie Sanders", "Nancy Pelosi", "Joe Biden"],
["Franz Ritz", "Angela Merkel", "Jan Schroeder", "John Stevens", "Karl Marx"],
["Wayne Gretsky", "Pierre Trudeau", "Mike Myers", "Justin Trudeau", "Justin Bieber"]
],
correctAnswer: [0, 3, 1, 3]
}];
$.ajax({
data: data,
dataType: "json",
success: function(result) {
alert("I'm in success function");
alert(result);
}
});