Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the HTTP status code with jQuery?

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/test/test.php",     data: {},     complete: function(xhr, statusText){     alert(xhr.status);      } }); 
like image 625
horgen Avatar asked Jun 02 '10 08:06

horgen


People also ask

How can I get status code from Ajax response?

//your jquery code will be like this: $. ajax({ type: 'POST', url: $url, data: new FormData(this), dataType: 'json', contentType: false, processData:false,//this is a must success: function(response){ $('your_selector'). html(response); } }); //php code will be like this echo '<div>some html code</div>'; die();

What is jqXHR in Ajax?

The jqXHR Object. The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method.


1 Answers

this is possible with jQuery $.ajax() method

$.ajax(serverUrl, {    type: OutageViewModel.Id() == 0 ? "POST" : "PUT",    data: dataToSave,    statusCode: {       200: function (response) {          alert('1');          AfterSavedAll();       },       201: function (response) {          alert('1');          AfterSavedAll();       },       400: function (response) {          alert('1');          bootbox.alert('<span style="color:Red;">Error While Saving Outage Entry Please Check</span>', function () { });       },       404: function (response) {          alert('1');          bootbox.alert('<span style="color:Red;">Error While Saving Outage Entry Please Check</span>', function () { });       }    }, success: function () {       alert('1');    }, }); 
like image 143
Ravi Mittal Avatar answered Oct 10 '22 11:10

Ravi Mittal