Categories
Development Java Javascript

Intercepting any request – javascript

I have a web page say – sample-page.html with 2 links in it –

sample-page.html
– link1 (GET, ajax)
– link 2 (GET, new page)

When I click link1 an ajax GET request is executed and it still stays at the sample-page.html.When I click link2 another get request is executed and a new page sample-page2 will appear.

To intercept both requests I’ve written a javascript code like this –

    //attempt-1 : javascript
    (function (send) {

        XMLHttpRequest.prototype.send = function () {
            this.setRequestHeader('some-header-param', 'some-value');
            send.apply(this, arguments);
        };

    })(XMLHttpRequest.prototype.send);

    //attempt-2: jquery 
    /*$.ajaxSetup({
        beforeSend: function (xhr,settings) {
           xhr.setRequestHeader('some-header', 'some-header-value');
       }
      }); */

The above, both attempts successfully able to intercept the ajax GET request from link1.
But neither of the above code snippets can’t able to intercept the request from link2. Can anyone help how to intercept both requests?

Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *