有没有办法在JavaScript中测试XMLHttpRequest是否对某个URL进行了测试?

时间:2014-09-11 17:36:34

标签: javascript xmlhttprequest

我知道如何在浏览器的开发工具中查看网络流量,并在控制台中显示XMLHttpRequests,但是是否有一个显示所有网络流量的窗口属性?

3 个答案:

答案 0 :(得分:1)

(function ($) {
  // The ajaxSend() method is a callback which is fired
      every time a jQuery AJAX call is completed. 
  $(document).ajaxSend(function(event, xhr, settings){ 
    if (iswantedurl(settings.url.pathname)){
        // ga('send','pageview',settings.url.pathname);
    }
  });
})(jQuery);
来自How to extend google analytics to track AJAX etc (as per H5BP docs)

原始代码

答案 1 :(得分:0)

使用jquery

如果你没有它,你可以扩展 XMLHttpRequest.prototype.open来解雇你的自定义事件。

答案 2 :(得分:0)

Pure JavaScript:

XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
        // Add my code to record all the requests
    if (!window.XMLLog) window.XMLLog = [];
    window.XMLLog.push(this);

        // Don't break the original JS
    return this.xhr.open(method, url, async, user, password);
}

jQuery的:

$(document).ajaxSend(function(event, xhr, settings){ 
  if (!window.XMLLog) window.XMLLog = [];
  window.XMLLog.push({event:event,xhr:xhr,settings:settings});
});