gpt4 book ai didi

javascript - AngularJS HTTP请求不会出现在错误中

转载 作者:行者123 更新时间:2023-12-03 08:53:18 25 4
gpt4 key购买 nike

我正在使用angularjs调用Rest Web服务,但是我遇到了错误处理问题。
这是我的http调用之一:

$http({
method: 'POST',
url: "tr"+licenseSelected,//angular need string in url
headers: {'Content-Type': 'application/json'},
data : updatedLicense,
beforeSend: function() {
waitingModal.showPleaseWait();
},
complete: function() {
setTimeout(function(){
waitingModal.hidePleaseWait();
}, 1000);
}
}).then(function successCallback(response) {
if (response.data.success==true){
licenseTable.ajax.reload();
$('#updateLicenseModal').modal("hide");
notifyMessage(response.data.result, 'success');
} else {
notifyMessage(response.data.result, 'error');
}
}, function errorCallback(response) {
window.location.href = "/ATS/500";
});

如果http请求期间发生错误(例如服务器关闭或错误的url),我想显示500页,但是永远不会调用errorCallback。
我的代码中有错误吗?我的错在哪里?
这是我无法使用错误代码处理的响应示例:
{
"status":422,
"exception":"org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message":"Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: \"tr71\"",
"stacktrace":"org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: \"tr71\"
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:115)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
......"
}

Web服务示例
@Override
@RequestMapping(value = { "/license"}, method = RequestMethod.POST)
public @ResponseBody Response createLicense(@RequestBody ClientLicenseForm clientLicenseForm) {
try{
administrationService.createLicense(clientLicenseForm);
return new Response(true, true, "Your license has been created!", null);
}catch(Exception e){
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in AdministrationControllerImpl::createLicense :" + errorResponse.getStacktrace());
return new Response(false,false,"Error! Your license hasn't been created!",errorResponse);
}
}

这可能是问题所在(将json响应包装在另一个对象中),但是我该如何解决?
enter image description here

更新
我已修复此代码,我将对其进行测试
}).then(function successCallback(response) {
if (typeof response.data.success == 'undefined'){
window.location.href = "/ATS/500";
}else if (response.data.success==true){
licenseTable.ajax.reload();
$('#updateLicenseModal').modal("hide");
notifyMessage(response.data.result, 'success');
} else if(response.data.success==false) {
notifyMessage(response.data.result, 'error');
}
}, function errorCallback(response) {
window.location.href = "/ATS/500";
});

最佳答案

重读您的评论会得到一个json响应(我认为带有200个 header )
header 响应为200代码时,错误回调不会触发。

您可以通过两种方式对其进行管理:

  • 重写后端以返回正确的状态头代码
  • 检查响应是否成功成功
    if (response.data.exception) {
    window.location.href = "/ATS/500";
    }

  • 注:,但是从根本上说服务器返回此类型或堆栈跟踪错误是错误的。

    Exceptions handling examples

    关于javascript - AngularJS HTTP请求不会出现在错误中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35478187/

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