I'm creating a simple JavaScript code to intercept any AJAX request with the idea of add an extra param that I need to read in the server side (Java). Until now I have this code that I have tested with different kind of request, with and without jquery as well, works fine:
(function(open) { XMLHttpRequest.prototype.send = function() { console.log('Start a new AJAX request'); var myExtraParam = 'ABC123'; // value that I need to send, but it's dynamically, it will come from a function open.apply(this); }; })(XMLHttpRequest.prototype.send); What I have been looking is the way to attach an extra parameter, on this scenario, the variable "myExtraParam" when I call the apply method.
I'm using plain JavaScript
EDIT: As param, I'm asuming a value that I can read in the server, like when you do: myurl?myExtraParam=ABC123
apply()is for sending an array as parameter.open()is called. So, you'll probably want to overrideopen()instead.open.apply(this);will discard the arguments originally passed tosend(). In such cases you should callopen.apply(this, arguments);