最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to handle "Failed to load resource" - Stack Overflow

programmeradmin4浏览0评论

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 mand 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

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 mand 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

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 8, 2014 at 10:17 Pavan Kumar kotaPavan Kumar kota 451 gold badge1 silver badge9 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

Your syntax is incorrect, and you probably miss a reference to jQuery, but anyway this code should be the remended 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("plete");
    });
}

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/server.html

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); }
}
发布评论

评论列表(0)

  1. 暂无评论