2016-03-06 95 views
0

我是angularJS的新手。下面的代碼在幾分鐘前工作正常。不知道我做了什麼改變,現在它給了我JSON.parse:JSON數據第1行第1列的數據意外結束 請你看看下面的代碼嗎?正在獲取syntaxError:JSON.parse:JSON數據第1行第1列的數據意外結束

<!doctype html> 

     <html lang="en" ng-app="myapp"> 
     <head> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0 /angular.min.js"></script> 
     <script src="controller.js"></script> 
    </head><body> 
    <div ng-controller="people"> 
    <ul> 
     <h2> I need to learn AngularJS </h2> 
     <li ng-repeat="person in persons"> 
      {{ person.name + ' : ' + person.age}} 
     </li> 
     </ul> 
    </div> 
    </body> 
    </html> 

JS文件

var app = angular.module('myapp', []); 
    app.controller('people' , function($scope, $http){ 
    $http.get('http://127.0.0.1/garg/database.json') 
    .success(function(response){ 
    $scope.persons = response.records; 
    }); 
    }); 

JSON文件

  { 
       "records":[ 
      { 
       "name": "rock", 
       "age" : "40" 

      }, 
      { 
       "name": "undertaker", 
       "age" : "40" 


      }, 
      { 
       "name": "kane", 
       "age" : "40" 


      }, 
      { 
       "name": "triple h", 
       "age" : "40" 


      } 
      ] 
      } 
+0

爲我工作。在用於angularjs的url中有2個額外的空格。這是複製/粘貼錯誤嗎? – pie

+0

是的,這是複製/粘貼錯誤。它也爲我工作。但現在我不知道爲什麼它會這樣。 :( – scripter

+0

請問你能否粘貼完整的錯誤信息? – pie

回答

1

你應該使用相同的域名/ IP作爲一個在你的AJAX調用訪問HTML文件。例如,在你的控制器使用:

var app = angular.module('myapp', []); 
app.controller('people' , function($scope, $http){ 
    $http.get('http://127.0.0.1/garg/database.json') 
    .success(function(response){ 
     $scope.persons = response.records; 
    }); 
}); 

以及與此網址訪問您的HTML文件:http://127.0.0.1/garg/index.html

+0

非常感謝:)。我正在使用localhost/garg/index.html訪問我的文件。沒有任何想法使用IP會造成這麼大的差異。 – scripter

相關問題