如何检查浏览器中是否打开了 HTTP 请求?

发布于 2025-01-05 12:16:03 字数 374 浏览 1 评论 0原文

有没有一种简单的方法可以检测 XMLHttpRequest 在浏览器窗口中是否处于活动状态?或者说有多少是活跃的? IE。有没有办法检测我的浏览器窗口中是否有任何 AJAX 调用处于活动状态?

问题扩展:使用 javascript 有没有办法查看是否有任何 XMLHttpRequests 打开?例如 "window.XMLisActive()" 或类似的东西?

解决方案:最终为 XMLHttpRequest 编写了一个包装器:gist here

Is there an easy way to detect if an XMLHttpRequest is active in the browser window? Or how many are active? ie. Is there a way to detect if there are any AJAX calls active in my browser window?

Extension of question: Using javascript is there a way I can see if any XMLHttpRequests are open? Such as "window.XMLisActive()" or something like that?

Solution: Ended up writing a wrapper for XMLHttpRequest: gist here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久随 2025-01-12 12:16:03

没有办法从 JS 检测打开的连接,除非您为 XmlHttpRequest 编写一个包装器(或对其进行猴子修补)来跟踪打开的连接。

这是kidcapital的猴子补丁,不确定它是否完美,但这是一个好的开始

  (function() {
    var oldOpen = XMLHttpRequest.prototype.open;
    window.openHTTPs = 0;
    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
      window.openHTTPs++;
      this.addEventListener("readystatechange", function() {
          if(this.readyState == 4) {
            window.openHTTPs--;
          }
        }, false);
      oldOpen.call(this, method, url, async, user, pass);
    }
  })();

注意

它不处理fetch或websockets,但在这些情况下你可以做类似的事情。

There is not a way to detect an open connection from JS unless you write a wrapper for XmlHttpRequest (or monkey patch it) that keeps track of opened connections.

Here's kidcapital's monkey patch, not sure if it's perfect, but it's a good start

  (function() {
    var oldOpen = XMLHttpRequest.prototype.open;
    window.openHTTPs = 0;
    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
      window.openHTTPs++;
      this.addEventListener("readystatechange", function() {
          if(this.readyState == 4) {
            window.openHTTPs--;
          }
        }, false);
      oldOpen.call(this, method, url, async, user, pass);
    }
  })();

Note

This does not handle fetch or websockets but you could do something similar in those cases.

茶色山野 2025-01-12 12:16:03

您可以使用 FireBug 来实现此目的。

在此处输入图像描述

You can use FireBug for that.

enter image description here

冰火雁神 2025-01-12 12:16:03

我尝试了 @juan-mendes 的解决方案并收到错误:错误错误代码:未定义消息:无法在“XMLHttpRequest”上设置“responseType”属性:无法更改从文档发出的同步请求的响应类型。< /代码>
在我的 Angular 应用程序中。原因是 async 参数,默认情况下该参数应该为 true,但是当该参数丢失时,此补丁会传递 undefined ,该参数会转换为 false。带有这个小更改的代码对我有用:

(function() {
        var oldOpen = XMLHttpRequest.prototype.open;
        window.openHTTPs = 0;
        XMLHttpRequest.prototype.open = function(method, url, async = true, user = null, pass = null) {
            window.openHTTPs++;
            this.addEventListener("readystatechange", function() {
                if(this.readyState == 4) window.openHTTPs--;
            }, false);
            oldOpen.call(this, method, url, async, user, pass);
        }
    })()

I tried @juan-mendes's solution and got an error: ERROR Error Code: undefined Message: Failed to set the 'responseType' property on 'XMLHttpRequest': The response type cannot be changed for synchronous requests made from a document.
in my Angular application. The cause is the async parameter which is supposed to be true by default, but when the argument is missing, this patch passes undefined which translates to false. The code with this small change works for me:

(function() {
        var oldOpen = XMLHttpRequest.prototype.open;
        window.openHTTPs = 0;
        XMLHttpRequest.prototype.open = function(method, url, async = true, user = null, pass = null) {
            window.openHTTPs++;
            this.addEventListener("readystatechange", function() {
                if(this.readyState == 4) window.openHTTPs--;
            }, false);
            oldOpen.call(this, method, url, async, user, pass);
        }
    })()
小红帽 2025-01-12 12:16:03

通过使用 jQuery,您可以跟踪全局事件处理程序是否有打开的请求:.ajaxStart().ajaxComplete()

在 ajaxStart 上设置并在 ajaxComplete 上重置的全局变量应该可以完成这项工作。

By using jQuery, you can keep track if there are open requests with the global event handlers: .ajaxStart() and .ajaxComplete()

A global variable set on ajaxStart and reset on ajaxComplete should do the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文