如何使用JavaScript存储"XHR完成加载" Chrome控制台中出现的错误消息? [英] How do I use JavaScript to store "XHR finished loading" messages in the console in Chrome?

查看:143
本文介绍了如何使用JavaScript存储"XHR完成加载" Chrome控制台中出现的错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用JavaScript存储执行xmlhttprequests时出现在Chrome控制台中的调试消息的方法.下面提供了示例输出:

I'm looking for a way to use JavaScript to store debug messages that appear in the Chrome console when xmlhttprequests are performed. Example output provided below:

提前谢谢!

推荐答案

您无法从JavaScript中读取控制台消息.您将无法阅读这些消息.

You cannot read console messages from JavaScript. You will not be able to read these messages.

但是,使用与 John Culviner 相同的一般概念来回答,您可以检测JavaScript中导致这些消息出现的事件.

However, using the same general concept as John Culviner's answer to Add a "hook" to all AJAX requests on a page, you can detect the events in JavaScript that cause these messages to appear.

(function() {
    var origOpen = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url) {
        this.addEventListener('load', function() {
            console.log('XHR finished loading', method, url);
        });

        this.addEventListener('error', function() {
            console.log('XHR errored out', method, url);
        });
        origOpen.apply(this, arguments);
    };
})();

这将使用新函数覆盖每个XHR对象的open方法,该函数将loaderror侦听器添加到XHR请求中.当请求完成或出现错误时,函数可以访问与open方法一起使用的methodurl变量.

This overwrites every XHR object's open method with a new function that adds load and error listeners to the XHR request. When the request completes or errors out, the functions have access to the method and url variables that were used with the open method.

如果愿意,您可以对methodurl做一些有用的事情,而不是简单地将它们传递给console.log.

You can do something more useful with method and url than simply passing them into console.log if you wish.

这篇关于如何使用JavaScript存储"XHR完成加载" Chrome控制台中出现的错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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