我和本主题中的人有同样的问题: $http.get doesn't load JSON
无论哪种方式都无法找到解决方案。 我使用MAMP运行网站,所以这不是问题吗?
以下是代码:
的index.html
<!DOCTYPE HTML>
<html ng-app="myQuiz">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Test Your Knowledge: Saturn</title>
<link rel="stylesheet" type="text/css" href="css/quiz.css">
<script type="text/javascript" ng-src="js/angular.min.js"></script>
<script type="text/javascript" ng-src="js/quiz.js"></script>
</head>
<body>
<div id="myQuiz" ng-controller="QuizController">
<h1>Test Your Knowledge: <span> Saturn</span></h1>
<div class="progress">
{{totalQuestions}}
</div>
</div>
</body>
</html>
quiz.js
(function(){
var app = angular.module('myQuiz', []);
app.controller('QuizController', ['$scope','$http','$sce',function($scope,$http, $sce){
$scope.score = 0;
$scope.activeQuestion = -1;
$scope.activeQuestionAnswered = 0;
$scope.percentage = 0;
$http.get('quiz_data.json').then(function(quizData){
$scope.myQuestions = quizData.data;
$scope.totalQuestions = $scope.myQuestions.length;
});
}]);
})();
quiz_data.json
[
{
"question" : "Saturn is the _______ planet from the Sun.",
"answers" : [
{ "id" : 0, "text" : "Fourth" },
{ "id" : 1, "text" : "Sixth" },
{ "id" : 2, "text" : "Second" },
{ "id" : 3, "text" : "Eighth" }
],
"correct" : 1
},
{
"question" : "Which image shows a close-up of Saturn?",
"answers" : [
{"id" : 0, "image" : "images/close_up_01.jpg" },
{"id" : 1, "image" : "images/close_up_02.jpg" },
{"id" : 2, "image" : "images/close_up_03.jpg" },
{"id" : 3, "image" : "images/close_up_04.jpg" }
],
"correct" : 3
},
{
"question" : "One year on Saturn is equivalent to how many years on Earth?",
"answers" : [
{"id" : 0, "text" : "12"},
{"id" : 1, "text" : "6"},
{"id" : 2, "text" : "29"},
{"id" : 3, "text" : "2"}
],
"correct" : 2
},
{
"question" : "What is the name of Saturn's largest moon?",
"answers" : [
{"id" : 0, "text" : "Hercules"},
{"id" : 1, "text" : "Europa"},
{"id" : 2, "text" : "Goliath"},
{"id" : 3, "text" : "Zeus"},
{"id" : 4, "text" : "Titan"},
{"id" : 5, "text" : "Triton"}
],
"correct" : 4,
"feedback" : "Though the names seem similar, Triton orbits the planet Neptune."
},
{
"question" : "Saturn is visible from Earth without a telescope",
"answers" : [
{"id" : 0, "text" : "True"},
{"id" : 1, "text" : "False"}
],
"correct" : 0
}
]
website 看起来与json的连接不起作用。不知道如何解决这个问题。
提前致谢!
答案 0 :(得分:0)
您必须通过本地http服务器转到您的文件。
在website
图片中,您似乎是通过文件系统而不是通过网络服务器加载index.html。
您的地址栏应为http://localhost:8888/<path to your index.html>
或运行您的网络服务器的任何端口。