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.



$.ajax({
                type: "POST",
                url: "Sample/Method",
                dataType:"html",
                success:function(response){
                  //process response object
                    alert("Success!!!");
                },
                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.


0 comments:

Post a Comment