我是angularJS的新手,我試圖用它從json文件中獲取數據。當我運行程序時,控制檯讀出404錯誤,並且該文件不存在。AngularJS說json文件不存在
這裏是我用來嘗試檢索json文件的角度。
var appLaptops = angular.module('appLaptops', []);
appLaptops.service("laptopsService", function ($http, $q) {
var deferred = $q.defer();
$http.get('json/laptops.json')
.then(function (data) {
deferred.resolve(data);
});
this.getLaptops = function() {
return deferred.promise;
}
})
appLaptops.controller('ctrlLaptops', function ($scope, laptopsService) {
var promise = laptopsService.getLaptops();
promise.then(function (data) {
$scope.laptop = data;
//For testing
console.log($scope.laptop);
})
});
下面我還將附加鏈接到文件夾結構的圖片以及錯誤讀數。
文件夾結構:http://i.imgur.com/qgQIdYf.jpg
錯誤讀數:http://i.imgur.com/c54VLiI.jpg
是你的'json'目錄公開?您是否嘗試先在普通瀏覽器中打開「http:// localhost:5237/json/laptops.json」? –
這也可能是一個問題:http://stackoverflow.com/questions/26699795/http-get-of-json-file-always-returns-404 – QueryLars