我希望我的servlet
返回包含数据的错误代码,因此客户端可以向用户指出请求有什么问题。
如果数据无效时exception
:
catch (UnvaildInputException UIE) {
UnvaildCodes unvaildCode = UIE.getUnvaildCode(); //code 1 for example
System.out.println(unvaildCode.getKey() + "-" + unvaildCode.getValue()); //prints error key-val
System.out.println(unvaildCode.getValue());
response.getWriter().print(HttpServletResponse.SC_BAD_REQUEST +":"+unvaildCode.getValue()); //trying to write a string as data
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); //400
}
现在在客户端(angularjs
)我正在尝试访问数据:
httpService.sendPost(vm.details, '/some/url').then(function (response){
$log.log(response);
$log.log(response.data);
});
错误:
TypeError: Cannot read property 'data' of undefined
at formDirectiveJS.js:91
at angular.min.js:126
at m.$eval (angular.min.js:140)
at m.$digest (angular.min.js:137)
at m.$apply (angular.min.js:141)
at g (angular.min.js:93)
at t (angular.min.js:98)
at XMLHttpRequest.u.onload (angular.min.js:99)
答案 0 :(得分:1)
您需要在错误处理程序中处理错误,而不是成功处理程序。
来自https://stackoverflow.com/a/35458437/1842905
$http.get(url)
.then(function (response) {
console.log('get',response)
})
.catch(function (data) {
// Handle error here
});