我是angularJS的新手。几分钟之前,下面的代码工作正常。不知道我做了哪些更改,现在它给了我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 :(得分:1)
您应该使用与AJAX调用中相同的域/ ip访问您的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