简体   繁体   English

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

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

I'm using jQuery to make an AJAX request.我正在使用 jQuery 发出 AJAX 请求。 I want to perform different actions whether or not the HTTP status code is a 400 error or a 500 error.无论 HTTP 状态码是 400 错误还是 500 错误,我都想执行不同的操作。 How can I achieve this?我怎样才能做到这一点?

$.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. @GeorgeCummins 提到与响应主体一起工作“似乎很奇怪”。 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?我在这里为此创建了另一个 StackOverflow 问题:当出现用户/表单验证错误时,我应该向 AJAX 请求发送什么响应/状态代码?

If you're using jQuery 1.5, then statusCode will 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.您应该会看到第一个警报中的状态代码。

You should create a map of actions using the statusCode setting:您应该使用statusCode设置创建一个 map 操作:

$.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.
    }
} 

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');
   }
  }

});

An other solution is to use the response.status function.另一种解决方案是使用 response.status function。 This will give you the http status wich is returned by the ajax call.这将为您提供 http 状态,该状态由 ajax 调用返回。

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

Here you can use this too it works for me在这里你也可以使用它,它对我有用

$.ajax({
  success: function(data, textStatus, xhr) {
    console.log(xhr.status);
  },
  complete: function(xhr, textStatus) {
    console.log(xhr.status);
  } 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM