如何阻止jQuery.ajax()将失败记录到控制台? [英] How can I stop jQuery.ajax() from logging failures to the console?

查看:506
本文介绍了如何阻止jQuery.ajax()将失败记录到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



<$ p $函数fetchServerStatus(服务,主机)
{
var port = serverStatus [service] .port;
$ .ajax({
url:http://+ host +:+ port +/ admin / server / status,
dataType:jsonp,
超时:1000,
错误:ajaxFail,
失败:ajaxFail,
成功:ajaxDone,
完成:ajaxDone,
完成:ajaxAlways,
总是:ajaxAlways
});
}

我遇到的问题是尽管为<$ c $声明了处理程序c>失败和错误,它们不会将任何内容记录到控制台,jQuery始终将失败的请求记录在控制台中。如果资源很长时间不可用,这会建立巨大的控制台日志,最终导致浏览器进程崩溃(我的情况为Chrome)。



有没有办法阻止它做到这一点?

解决方案

当chrome的控制台中的日志条目是任何HTTP请求处理,而不是jQuery或ajax(XMLHttpRequest)的问题,即使是< img> < link>

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


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.

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

Is there any way to stop it from doing this?

解决方案

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.

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.

这篇关于如何阻止jQuery.ajax()将失败记录到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆