2016-01-06 81 views
-2

在下面的代码,angularjs控制器无法读取JSON文件

/* app.js*/ 
var app = angular.module('Sample', []); 

/* Controller.js*/ 
app.controller('Controller', function($scope, $http){ 
    $http.get('result.json'). 
      then(function(data) {$scope.tabular_data = data;} , function(data) {console.log("My error: " + data)}); 


}); 

[{ 
    "name": "Brokerage Account 3", 
    "marketValue": "1999990", 
    "cash": "1995826", 
    "legend": "orange" 
}, { 
    "name": "Account 3", 
    "marketValue": "1949990", 
    "cash": "1695856", 
    "legend": "darkorange" 
}] 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Sample app</title> 

    <link rel="stylesheet" href="app_style.css"> 

    <!-- Libs --> 
    <script src="angular.js"></script> 

    <!-- Application --> 
    <script src="app.js"></script> 
    <script src="Controller.js"></script> 
</head> 
<body ng-app="Sample"> 
    ..... 
    <!-- Content --> 
    <div id="content" ng-controller="Controller"></div> 
</body> 

控制器代码无法读取json文件。

在线工具验证JSON文件是有效的文件

开发工具控制台没有显示任何错误。

如何解决此错误?

+0

你可以添加你的html吗? –

+0

@PankajParkar请找到HTML – overexchange

+1

在一个完全不同的音符,JSON数据不应该被后“值”有',':“XXX” – venkatKA

回答

0

我猜可能是您的JSON的问题。尝试不结束逗号,

[{ 
    "name": "Name1", 
    "Value": "1000" 
}, { 
    "name": "Name2", 
    "Value": "1949990" 

}] 

每当使用JSON总是验证它们在http://jsonlint.com/

Demo Plunkr

+2

如果上面是problem..then他应该得到错误的console..but他没有得到控制台错误.. –

+0

我知道你是正确的,但好奇如何将不会引发错误.. –

+1

呀。这很奇怪。我之前有过类似的情况。所以我想这可能是json linting错误。我看到了你的普朗克。在添加了这些额外的逗号之后,没有任何输出。 –

0

线似乎你错过了在最后一个右括号。试试这个:

app.controller('Controller', function($scope, $http) { 
     $http.get('result.json') 
     .then(
      function(data) { 
       //$scope.tabular_data = data; 
       console.log("Success!"); 
      }, 
      function(data) { 
       console.log("My error: " + data) 
      } 
     ); 
});