我是 angularjs 的新手。我正在使用 angularjs 1.4 版本。我正在尝试处理从 Spring Controller 方法抛出的异常。但是我无法在下面的 js 中捕获异常。
可能这是重复的问题,但我已经在 SO 中搜索了解决方案。但是没有用。请给我建议。提前致谢。
在 Spring MVC Controller 方法中也抛出异常是否正确?或者如何向前端说 500 错误?
在 Angular Controller 中:
Service.calculate (obj).then(function(response){
// logic
});
在 Angular 服务中:
this.calculate = function(obj){
thử {
return $http.post(PATH+'txn/calc',obj).then(function(resp){
return resp;
});
} catch (e) {
alert(e);
}
};
Spring Controller 方法:
@RequestMapping(value = "/txn/calc", method = RequestMethod.POST)
public @ResponseBody someBean calculate(@RequestBody SomeBean someBean , HttpServletRequest request) {
thử {
// exception occurs here
} catch (Exception e) {
throw e;
}
}
Hãy thử điều này:
$http.post(url).then(function(response) {
console.log('post',response)
},
function(data) {
// Handle error here
})
Tôi là một lập trình viên xuất sắc, rất giỏi!