javascript 如何处理“加载资源失败”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26254467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:43:39  来源:igfitidea点击:

How to handle "Failed to load resource"

javascriptjquery

提问by Pavan Kumar kota

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

我正在编写 Javascript 以了解不同的站点(开发、登台和生产)是否正常运行。下面是我的代码。

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.

我遇到无效 URL 的问题,无效 URL 的浏览器命令中显示以下错误。

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

Failed to load resource: net::ERR_NAME_NOT_RESOLVED

加载资源失败:服务器响应状态为 404(未找到)

无法加载资源: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.

以及那些我无法在任何地方(try/catch、error 或 404)捕获它们的错误。有人可以帮助我在哪里(方法或级别)我可以处理这些错误。

Thanks, KPK

谢谢,KPK

回答by Faris Zacina

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:

您的语法不正确,您可能错过了对 jQuery 的引用,但无论如何,这段代码应该是较新的 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.

但是,请记住,您可能会执行跨域请求(除非您要检查的源站点和目标站点位于同一域中),并且根据您的服务器配置,该请求可能会被阻止。因此,您必须在要从其他地方 ping 的服务器上启用 CORS。

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

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

回答by Mihir

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

ajax 方法选项有一个额外的右括号“}”

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); }
}