Script to check error message from ajax call

Ajax call captures error message/exception that are thrown on its failure. Below is the way to get that error message.

                type: "POST",
                url: "Sample/Method",
                  //process response object
                error:function (xhr, ajaxOptions, thrownError){                                
                alert(xhr.status); //get error status code                  
                alert(thrownError); //get error message

                alert(xhr.responseText); //get error details

Below is the breif description of these error terms:
  • xhr.status -- Use this when you have to deal with the error status code, for example session expiration.
  • thrownError -- Use this when you want to display/use error message as it is.
  • xhr.responseText -- Gives you details of error message like cause of error, source of error, etc. Helpful for coders as they come to know cause of error at early stage.


Post a Comment