How to get the error code when an error occurs in an ajax request

such as 404 500, these


you can give an example of jquery's ajax. Take a look at

.
$(document).ready(function() {
  jQuery("-sharpclearCac").click(function() {
     jQuery.ajax({
       url: url,
       type: "post",
       data: { id: '0' },
       dataType: "json",
       success: function(msg) {
         alert(msg);
       },
       error: function(XMLHttpRequest, textStatus, errorThrown) {
         alert(XMLHttpRequest.status);
         alert(XMLHttpRequest.readyState);
         alert(textStatus);
       },
       complete: function(XMLHttpRequest, textStatus) {
         this; // AJAXoptions
       }
     });
  });
});

when the asynchronous call through ajax is successful, the success function is called. The syntax of the success function is

//:
function (data, textStatus){
     // data could be xmlDoc, jsonObj, html, text, etc...   
     this;
    // the options for this ajax request
}

the error function is called when an error occurs in an asynchronous call through ajax. The syntax of the error function is

//( :  (xml  html)) 
//:XMLHttpRequest 
//null
//"timeout", "error", "notmodified"  "parsererror"
  
//textStatus: "timeout", "error", "notmodified"  "parsererror"
 
error:function (XMLHttpRequest, textStatus, errorThrown) 
{ 
  
} 

the first parameter XMLHttpRequest:
XMLHttpRequest.readyState: status code returned by the

error event
0-(uninitialized) has not yet called the send () method
1-(loaded) has called the send () method, and is sending a request
2-(loading completed) send () method execution completed, all response content
3-(interaction) is parsing response content
4-(completion) response content analysis is complete. You can call

on the client side.

XMLHttpRequest.status represents the status of the current http request, such as 200404500


get the status property of the xhr object, which is the status code

Menu