Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to do exception handling in Ajax ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The purpose of this article is to demonstrate how we handle the exception in the jQuery AJAX request. A basic understanding of HTML, CSS,  and jQuery is required. This can be done by an AJAX fail() method. We discuss 3 AJAX methods to better understand what’s going on when making any ajax() request from our web browser to a particular server.

AJAX: AJAX is an acronym for “Asynchronous JavaScript and XML”. The Ajax component exploits this ability of JavaScript to send asynchronous HTTP requests, receive the XML response (as well as other formats), and update part of a website (using JavaScript) without reloading or refreshing the entire site.

The three methods that we need to know to make AJAX requests are as follows.

This method is called when an HTTP request is successful.


This method is called when an HTTP request fails.


This method is called always, be the HTTP request fails or is successful.


Example: We are going to see how to use AJAX fail() methods to handle the error in the HTTP requests. The fail() callback takes 3 parameters where the first parameter is a JSON error object, the second parameter is given a reason in text format and the last parameter is for the error thrown by the HTTP request. The whole object with values of the different parameters is shown in the text area. The output is shown in JSON format to show you the value and type of the value that is received as a  parameter when an HTTP request fails.

The format of the output is as follows.

"firstparam": {
    value: -> the value of the first parameter
    type:  -> the type of the first parameter
"secondparam": {
    value: -> value of second parameter
    type:  -> the type of the second parameter
" thirdparam": {
    value: -> value of third parameter
    type:  -> the type of the third parameter

Example: In this example, we will see how to handle exceptional handling AJAX.


<!DOCTYPE html>
    <script type="text/javascript" src=
        .container {
            display: flex;
            justify-content: center;
        h1 {
            color: green;
        textarea {
            margin-top: 10px;
            width: 300px;
            height: 300px;
    <script type="text/javascript">
        $(document).ready(function () {
            var obj = "";
            $.ajax("gfg.txt").done(function () {
            }).fail(function (errorobj, textstatus, error) {
                obj = JSON.stringify({
                    firstparam: {
                        value: errorobj,
                        type: typeof (errorobj)
                    secondparam: {
                        value: textstatus,
                        type: typeof (textstatus)
                    thirdparam: {
                        value: error,
                        type: typeof (error)
                }, undefined, 1);
            }).always(function () {
    <h1 class="container">
    <div class="container">


My Personal Notes arrow_drop_up
Last Updated : 10 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials