jQuery:如何从 $.ajax.error 方法中获取 HTTP 状态代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6700822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:15:10  来源:igfitidea点击:

jQuery: How to get the HTTP status code from within the $.ajax.error method?

jquery

提问by Andrew

I'm using jQuery to make an AJAX request. I want to perform different actions whether or not the HTTP status code is a 400 error or a 500 error. How can I achieve this?

我正在使用 jQuery 发出 AJAX 请求。无论 HTTP 状态代码是 400 错误还是 500 错误,我都想执行不同的操作。我怎样才能做到这一点?

$.ajax({
    type: 'POST',
    url: '/controller/action',
    data: $form.serialize(),
    success: function(data){
        alert('horray! 200 status code!');
    },
    error: function(data){
        //get the status code
        if (code == 400) {
            alert('400 status code! user error');
        }
        if (code == 500) {
            alert('500 status code! server error');
        }
    },
});

Update:

更新:

@GeorgeCummins mentioned that it "seemed odd" to work with the response body. This is the first time that I've attempted doing this sort of thing. Is my approach not a best-practice? What would you recommend? I created another StackOverflow question for this here: What response/status code should I send to an AJAX request when there is a user/form validation error?

@GeorgeCummins 提到与响应主体一起工作“似乎很奇怪”。这是我第一次尝试做这种事情。我的方法不是最佳实践吗?你会推荐什么?我在这里创建了另一个 StackOverflow 问题:当出现用户/表单验证错误时,我应该向 AJAX 请求发送什么响应/状态代码?

回答by Alex Reynolds

If you're using jQuery 1.5, then statusCodewill work.

如果您使用的是 jQuery 1.5,则statusCode可以使用。

If you're using jQuery 1.4, try this:

如果你使用 jQuery 1.4,试试这个:

error: function(jqXHR, textStatus, errorThrown) {
    alert(jqXHR.status);
    alert(textStatus);
    alert(errorThrown);
}

You should see the status code from the first alert.

您应该会看到第一个警报中的状态代码。

回答by George Cummins

You should create a map of actions using the statusCodesetting:

您应该使用以下statusCode设置创建操作映射:

$.ajax({
  statusCode: {
    400: function() {
      alert('400 status code! user error');
    },
    500: function() {
      alert('500 status code! server error');
    }
  }
});

Reference(Scroll to: 'statusCode')

参考(滚动到:'statusCode')

EDIT(In response to comments)

编辑(回应评论)

If you need to take action based on the data returned in the response body (which seems odd to me), you will need to use error:instead of statusCode:

如果您需要根据响应正文中返回的数据采取行动(这对我来说似乎很奇怪),您将需要使用error:而不是statusCode:

error:function (xhr, ajaxOptions, thrownError){
    switch (xhr.status) {
        case 404:
             // Take action, referencing xhr.responseText as needed.
    }
} 

回答by CodeWhisperer

An other solution is to use the response.status function. This will give you the http status wich is returned by the ajax call.

另一种解决方案是使用 response.status 函数。这将为您提供 ajax 调用返回的 http 状态。

function checkHttpStatus(url) {     
    $.ajax({
        type: "GET",
        data: {},
        url: url,
        error: function(response) {
            alert(url + " returns a " + response.status);
        }, success() {
            alert(url + " Good link");
        }
    });
}

回答by genesis

use

   statusCode: {
    404: function() {
      alert('page not found');
    }
  }

-

——

$.ajax({
    type: 'POST',
    url: '/controller/action',
    data: $form.serialize(),
    success: function(data){
        alert('horray! 200 status code!');
    },
    statusCode: {
    404: function() {
      alert('page not found');
    },

    400: function() {
       alert('bad request');
   }
  }

});