Javascript 如何阻止 jQuery.ajax() 将故障记录到控制台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11949095/
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-08-24 07:47:18  来源:igfitidea点击:

How can I stop jQuery.ajax() from logging failures to the console?

javascriptjquerygoogle-chrome

提问by Will Hains

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 failand 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).

我遇到的问题是,尽管为failand声明了处理程序error,它们不会将任何内容记录到控制台,但 jQuery 总是在控制台中记录失败的请求。如果资源长时间不可用,这会建立一个巨大的控制台日志,最终使浏览器进程崩溃(在我的例子中是 Chrome)。

Is there any way to stop it from doing this?

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

回答by otakustay

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 控制台中的日志条目是 chrome 在处理任何 HTTP 请求时的行为,不是 jQuery 或 ajax(XMLHttpRequest) 的问题,即使是<img>or<link>标签也可能导致此问题。

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 未找到或内容类型错误),因此根据记录到控制台的内容,也许可以找到更好的解决方案,请提供控制台中的准确日志消息。

回答by Fstarocka Burns

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.

它是暂时的 - 但对我有用。击败必须滚动 45 行废话来调试 js 错误。