如何阻止jQuery.ajax()将失败记录到控制台?

时间:2022-10-07 17:19:21

I am using the following jQuery JSONP request to check the status of a resource by URL. When the resource is not available or the server is down, my ajaxFail() function updates the display.

我使用以下jQuery JSONP请求通过URL检查资源的状态。当资源不可用或服务器关闭时,我的ajaxFail()函数更新显示。

function fetchServerStatus(service, host)
{
    var port = serverStatus[service].port;
    $.ajax({
        url: "http://" + host + ":" + port + "/admin/server/status",
        dataType: "jsonp",
        timeout: 1000,
        error: ajaxFail,
        fail: ajaxFail,
        success: ajaxDone,
        done: ajaxDone,
        complete: ajaxAlways,
        always: ajaxAlways
    });
}

The problem I'm having is that despite declaring handlers for fail and error, which do not log anything to the console, jQuery always logs the failed request in the console. If the resource is unavailable for a long time, this builds up a huge console log, eventually crashing the browser process (Chrome in my case).

我遇到的问题是,尽管声明了处理程序的失败和错误,但jQuery始终在控制台中记录失败的请求。如果资源长期不可用,这将建立一个巨大的控制台日志,最终导致浏览器进程崩溃(在我的例子中是Chrome)。

Is there any way to stop it from doing this?

有什么办法阻止它这样做吗?

2 个解决方案

#1


45  

The logging entry in chrome's console is a behaviour of chrome when any HTTP request is handled, not a problem with jQuery or ajax(XMLHttpRequest), even an <img> or <link> tag could cause this issue.

chrome控制台中的日志记录条目是处理任何HTTP请求时的chrome行为,而不是jQuery或ajax(XMLHttpRequest)的问题,甚至如何阻止jQuery.ajax()将失败记录到控制台?标记都可能导致这个问题。

Your only choice is to fix your server-side code not to trigger an error for ajax (eg. 404 not found or wrong content-type), so based on the content logged to console, maybe a better solution could be found, please provide accurate logging message in your console.

您唯一的选择是修复服务器端代码,不触发ajax错误(例如)。404未找到或内容类型错误),基于登录到控制台的内容,也许可以找到更好的解决方案,请在您的控制台提供准确的日志消息。

#2


0  

put this in the top of your page:

把这个放在你的页面顶部:

<script>

$(document).ready(function() {
jQuery.migrateMute = true;
})
</script>

its temporary - but worked for me. Beats having to scroll through 45 lines of nonsense to debug js errors.

这只是暂时的,但对我来说很有用。要调试js错误,必须遍历45行无意义的代码。

#1


45  

The logging entry in chrome's console is a behaviour of chrome when any HTTP request is handled, not a problem with jQuery or ajax(XMLHttpRequest), even an <img> or <link> tag could cause this issue.

chrome控制台中的日志记录条目是处理任何HTTP请求时的chrome行为,而不是jQuery或ajax(XMLHttpRequest)的问题,甚至如何阻止jQuery.ajax()将失败记录到控制台?标记都可能导致这个问题。

Your only choice is to fix your server-side code not to trigger an error for ajax (eg. 404 not found or wrong content-type), so based on the content logged to console, maybe a better solution could be found, please provide accurate logging message in your console.

您唯一的选择是修复服务器端代码,不触发ajax错误(例如)。404未找到或内容类型错误),基于登录到控制台的内容,也许可以找到更好的解决方案,请在您的控制台提供准确的日志消息。

#2


0  

put this in the top of your page:

把这个放在你的页面顶部:

<script>

$(document).ready(function() {
jQuery.migrateMute = true;
})
</script>

its temporary - but worked for me. Beats having to scroll through 45 lines of nonsense to debug js errors.

这只是暂时的,但对我来说很有用。要调试js错误,必须遍历45行无意义的代码。