How do I get the HTTP status code with jQuery?

前端 未结 9 1680
逝去的感伤
逝去的感伤 2020-11-27 13:34

I want to check if a page returns the status code 401. Is this possible?

Here is my try, but it only returns 0.

$.ajax({
    url: \"http://my-ip/tes         


        
9条回答
  •  情书的邮戳
    2020-11-27 14:37

    The third argument is the XMLHttpRequest object, so you can do whatever you want.

    $.ajax({
      url  : 'http://example.com',
      type : 'post',
      data : 'a=b'
    }).done(function(data, statusText, xhr){
      var status = xhr.status;                //200
      var head = xhr.getAllResponseHeaders(); //Detail header info
    });
    

提交回复
热议问题