Angularjs http请求没有进入errorCallback

时间:2016-02-18 09:57:30

标签: javascript angularjs http error-handling

我使用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";
        });

1 个答案:

答案 0 :(得分:0)

贬低你的评论你会得到一个json响应(我认为有200个标题) 在标题响应中是200代码时,错误回调不会触发。

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

  1. 重写后端以返回正确的状态标题代码

  2. 检查successCallback中的响应是否有效

    if (response.data.exception) {
        window.location.href = "/ATS/500";
    }
    
  3. 注意:但服务器使用stacktrace返回此类型或错误基本上是错误的。

    Exceptions handling examples