Pavan Kumar kota
Pavan Kumar kota

Reputation: 45

How to handle "Failed to load resource"

I am writing Javascript to know the different sites(dev, staging and prod) are UP or not. below is my code.

function URLStatusCheck(url)
{
try {
    $.ajax(url,
    {
        contentType: "application/json; charset=utf-8",
        dataType: 'jsonp',
        crossDomain: true,
        statusCode: {
            404: function (xhr) {
                 alert("error 404");
            },
            200: function (xhr) {
                   alert("Site is UP");
                }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("error");
         }
    
        }
    }).fail(function (jqXHR, textStatus, errorThrown) {
        alert("Fail");
    });
} catch (err) { alert(url); }
}

I have problem with invalid URLs, below error are showing in the browser command for the invalid URLs.

Failed to load resource: the server responded with a status of 404 (Not Found)

Failed to load resource: net::ERR_NAME_NOT_RESOLVED

and those errors I am not able to catch them in any where (try/catch, error or 404). Could some one help me where(method or level) I can handled those errors.

Thanks, KPK

Upvotes: 2

Views: 21802

Answers (2)

Faris Zacina
Faris Zacina

Reputation: 14274

Your syntax is incorrect, and you probably miss a reference to jQuery, but anyway this code should be the recommended way of error handling in newer JQuery > 1.8:

 function URLStatusCheck(url) {
    $.ajax(url, {
        contentType: "application/json; charset=utf-8",
        dataType: 'jsonp',
        crossDomain: true
    })
        .done(function (data, textStatus, jqXHR) {
        alert("success");
    })
        .fail(function (jqXHR, textStatus, errorThrown) {
        alert("error");
    })
        .always(function () {
        //alert("complete");
    });
}

However, have in mind that you are potentially going to do a cross-origin request (unless the source and destination sites you want to check are on the same domain), and depending on your server configuration the request could be blocked. So you will have to enable CORS on the servers you want to ping from elsewhere.

http://enable-cors.org/server.html

Upvotes: 3

Mihir
Mihir

Reputation: 449

There is an extra closing bracket "}" for ajax method options

function URLStatusCheck(url) {
    try {
        $.ajax(url,
        {
            contentType: "application/json; charset=utf-8",
            dataType: 'jsonp',
            crossDomain: true,
            statusCode: {
                404: function (xhr) {
                    alert("error 404");
                },
                200: function (xhr) {
                    alert("Site is UP");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("error");
            }

            //} -- the extra one
        }).fail(function (jqXHR, textStatus, errorThrown) {
            alert("Fail");
        });
    } catch (err) { alert(url); }
}

Upvotes: 1

Related Questions