【发布时间】:2017-03-28 23:46:38
【问题描述】:
我需要在 java 类中调用index() 方法。但是我试过这种方法它不起作用。
在 http 路径无法识别之后,它会上升到 controller.js 中的 console.log('coming here....');。
@RestController
public class DatumBoxShedule {
@Autowired
private DatumService datumService;
@RequestMapping(value = "/loadIndex", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public String index() throws IOException {
}
}
controller.js
app.controller('datumBoxShedule', function($scope, $http) {
$scope.newTodo = {};
$scope.loadIndex = function(){
console.log('coming here....');
$http.get('loadIndex')
.success(function(data, status, headers, config) {
$scope.todos = data;
})
.error(function(data, status, headers, config) {
alert('Error loading DatumBoxShedule');
});
};
$scope.loadIndex();
});
【问题讨论】:
-
尝试检查状态码,可能是 404,这意味着您需要提供绝对路径:
$http.get('/loadIndex')...。您可以直接从浏览器访问 Rest API 端点,也可以使用 Postman 等浏览器扩展来检查请求/响应。 -
我尝试了 $http.get('/loadIndex') 但仍然出现同样的错误。
-
后端 REST 服务的基本上下文是什么?通常你会有类似
http://host:port/*myApp*/loadIndex -
我的上下文是localhost:8080/loadIndex
标签: java angularjs spring hibernate