gpt4 book ai didi

javascript - Angularjs HTTP 方法中的异常处理

转载 作者:行者123 更新时间:2023-11-29 04:29:23 26 4
gpt4 key购买 nike

我是 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){
try {
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) {
try {
// exception occurs here
} catch (Exception e) {
throw e;
}
}

最佳答案

试试这个:

$http.post(url).then(function(response) {
console.log('post',response)
},
function(data) {
// Handle error here
})

关于javascript - Angularjs HTTP 方法中的异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44453154/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com