我在这里想做的是,我想使用使用 Spring Restful WebService 生成的 JSON,如下所示:
[
{
"userid": 1,
"firstName": "kevin",
"lastName": "buruk",
"email": "[email protected] /cdn-cgi/l/email-protection"
},
{
"userid": 2,
"firstName": "helm",
"lastName": "krpuk",
"email": "[email protected] /cdn-cgi/l/email-protection"
},
{
"userid": 3,
"firstName": "batok",
"lastName": "kelapa",
"email": "[email protected] /cdn-cgi/l/email-protection"
}
]
该 JSON 是由我的 Java Spring MVC 控制器中的此函数生成的,如下所示:
SpringController.java
@RestController
@RequestMapping("/service/")
public class SpringServiceController {
UserService userService = new UserService();
@RequestMapping(method = RequestMethod.GET,headers="Accept=application/json")
public List<User> getAllUsers() {
List<User> users=userService.getAllUsers();
return users;
}
}
我即将将 JSON 值消费到角度表中,因此我将 JSON 值存储在范围内的角度对象中,这是我的 Javascript 代码。我将此文件命名为 app.js
app.js
function Hello($scope, $http) {
$http.get('http://localhost:8080/SpringServiceJsonSample/service/').
success(function(data) {
$scope.users = data;
});
}
这是我的 html 页面。我将其命名为index.html
索引.html
<html ng-app>
<head>
<title>Angular Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="Hello">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<td>{{user.userid}}</td>
<td>{{user.firstName}}</td>
<td>{{user.lastName}}</td>
<td>{{user.email}}</td>
</tr>
</tbody>
</div>
</body>
</html>
我在这里错过了什么?我不会展示任何东西。在此之前,我尝试使用只有一条记录的 JSON,它工作正常。但这次我尝试消耗数组,但失败了。我在这里错过了什么?是 JSON 还是我的 javascript?