Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
104
rated 0 times [  106] [ 2]  / answers: 1 / hits: 26845  / 13 Years ago, sat, october 15, 2011, 12:00:00

How would I be able to override the XMLHttpRequest.open() method and then catch and alter it's arguments?


I've already tried the proxy method but it didn't work, although removing the open over-rid when XMLHttpRequest() was called:


(function() {
var proxied = window.XMLHttpRequest.open;
window.XMLHttpRequest.open = function() {
$('.log').html(arguments[0]);
return proxied.apply(this, arguments);
};
})();

More From » ajax

 Answers
7

You are not modifying the open method inherited by XMLHttpRequest objects but just adding a method to the XMLHttpRequest constructor which is actually never used.



I tried this code in facebook and I was able to catch the requests:



(function() {
var proxied = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function() {
console.log( arguments );
return proxied.apply(this, [].slice.call(arguments));
};
})();

/*
[POST, /ajax/chat/buddy_list.php?__a=1, true]
[POST, /ajax/apps/usage_update.php?__a=1, true]
[POST, /ajax/chat/buddy_list.php?__a=1, true]
[POST, /ajax/canvas_ticker.php?__a=1, true]
[POST, /ajax/canvas_ticker.php?__a=1, true]
[POST, /ajax/chat/buddy_list.php?__a=1, true]
*/


So yeah the open method needs to be added to XMLHttpRequest prototype (window.XMLHttpRequest.prototype) not XMLHttpRequest constructor (window.XMLHttpRequest)


[#89602] Thursday, October 13, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
grayson

Total Points: 36
Total Questions: 113
Total Answers: 95

Location: Tonga
Member since Fri, Aug 21, 2020
4 Years ago
grayson questions
;