如何在没有控制台错误的情况下检查资源是否存在?

时间:2013-02-07 10:42:40

标签: javascript ajax

我想检查资源是否存在而没有控制台错误。

我试过了:

jQuery.ajax({
    type : 'HEAD',
    url : myUrl,
    success : function(){},
    error : function(jqXHR, textStatus, errorThrown) {
        // business logic
    }
}).complete(function() {
    // business logic
});

它完美无缺,但在控制台中跟踪错误。

1 个答案:

答案 0 :(得分:0)

通过代码

写入控制台的Ajax错误
xhr.send( ( s.hasContent && s.data ) || null );

jquery-1.8.3中的8434行

所以你可以覆盖这段代码:

$(function () {

var xhr = null;

if (window.XMLHttpRequest) {
    xhr = window.XMLHttpRequest;
}
else if (window.ActiveXObject('Microsoft.XMLHTTP')) {
    xhr = window.ActiveXObject('Microsoft.XMLHTTP');
}

var send = xhr.prototype.send;
xhr.prototype.send = function (data) {
    try {
        //uncomment this line to write the error to console
        //send.call(this, data);

        //you can write custom error to console
        console.log('your error message');
    }
    catch (e) {
    }
};
});