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

后端 未结 4 1201
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 16:44

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?

4条回答
  •  佛祖请我去吃肉
    2020-12-02 17:11

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

提交回复
热议问题