How to view the last GET HTTP request in JavaScript

ε祈祈猫儿з 提交于 2019-11-29 10:24:13

You may want to modify the XMLHttpRequest.prototype.open to decorate it with your "tracking" code. Something like this:

var ajaxCalls = [];

XMLHttpRequest.prototype._originalOpen = XMLHttpRequest.prototype.open;

XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
   ajaxCalls.push(url);
   this._originalOpen(method, url, async, user, password);
}

The ajaxCalls array will get populated with the URLs of your Ajax requests. The last request will be at ajaxCalls[ajaxCalls.length - 1].

Note that if you want to track only GET requests, you can simply log URLs only if method === 'GET' in the open() method.

Daniel's solution works, but not for IE8 and lower, as IE has different number of arguments.

Try this instead:

window.XMLHttpRequest.prototype._original_open = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function() {
    ajaxCalls.push(arguments[1]);
    this._original_open.apply(this,arguments);
};
themerlinproject

I ended up going with a workaround using $_SERVER['REQUEST_URI'] in PHP and then passing it to JavaScript.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!