2015-05-19 47 views
2

我是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

+1

是你的'json'目錄公開?您是否嘗試先在普通瀏覽器中打開「http:// localhost:5237/json/laptops.json」? –

+0

這也可能是一個問題:http://stackoverflow.com/questions/26699795/http-get-of-json-file-always-returns-404 – QueryLars

回答

0

這個問題是不相關的角度。您有404錯誤,因爲IIS不知道如何處理laptops.json文件。

有兩個選項來解決這個問題:

  1. 更新的web.config讓IIS瞭解(由QueryLars在評論中提到)這種文件的內容類型:

$http.get of json file always returns 404

  • 配置HttpRoutes或HttpHandlers的
  • +0

    謝謝,這解決了我的錯誤。我會投它,但它說我需要15聲望。不過謝謝你。 –

    +0

    歡迎您:) – Warlock