使用jquery从ajax响应中获取Http状态代码

问题描述:

在我目前的春季项目中,当我向服务器提交表单时,响应由此方法处理:

In my current spring project, when I submit a form to server, the response is handled by this method:

    $('form.form').each(function () {
        var form = this;
        $(form).ajaxForm(function (data) {
            form.reset();
            $(".alert-info").find("#alert").html(data);
            $(".alert-info").show();
        });
    });

在我的控制器中,提交是通过以下方法处理的:

In my controller, the submission is handled by a method like this:

@RequestMapping(value="cadastra", method=RequestMethod.POST)
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
public void cadastra(@ModelAttribute("object") E object, BindingResult result, @RequestParam(value="file", required=false) MultipartFile file, @RequestParam(value="icone", required=false) MultipartFile icone, @RequestParam(value="screenshot", required=false) MultipartFile screenshot[]) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
    serv.cadastra(object);
    serv.upload_picture(object, file, "picture");
    serv.upload_picture(object, icone, "icone");
}

来自控制器的方法的错误响应由此ControllerAdvice类处理:

Error responses from the methods from the controller are handled by this ControllerAdvice class:

@ControllerAdvice
@PropertySource({"classpath:error.properties"})
public class GlobalDefaultExceptionHandler {

    @Autowired
    private Environment env;

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        // If the exception is annotated with @ResponseStatus rethrow it and let
        // the framework handle it - like the OrderNotFoundException example
        // at the start of this post.
        // AnnotationUtils is a Spring Framework utility class.
        if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
            throw e;
        // Otherwise setup and send the user to a default error-view.
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.addObject("url", req.getRequestURL());
        mav.addObject("msg", e.getLocalizedMessage());
        mav.setViewName("erro");
        return mav;
    }

}

我正在寻找一种方法在我的jquery代码中从响应中读取http状态代码(可以是1xx,2xx,3xx,4xx或5xx),并根据此代码显示相关消息。

I am looking for a way to read the http status code from response (which can be 1xx, 2xx, 3xx, 4xx or 5xx) in my jquery code, and display a related message according to this code.

在浏览器的网络监视器中,我可以看到成功的响应已经在方法中实现了代码HTTP 201;当发生错误时,响应应该有一个代码4xx或5xx,具体取决于触发的异常。

In the network monitor from browser, I can see a successful response already have the code HTTP 201 as implemented in the method; when an error occurs, the response should have a code 4xx or 5xx, depending from exception triggered.

这样,我想知道是否有人可以提示如何修改我的jquery代码和我的COntrollerAdvice来完成这个。

In this way, I wonder if anyone can give a hint of how modify my jquery code and my COntrollerAdvice to accomplish this.

看起来你正在使用 jQuery Form Plugin 。如果是这样,回调函数的第三个参数是 xhr 对象,你可以得到这样的HTTP状态:

It looks like you are using the jQuery Form Plugin. If so, the third parameter to the callback function is the xhr object, and you can get the HTTP status like this:

$(form).ajaxForm(function (data, statusText, xhr) {
    alert(xhr.status);

jsfiddle