P
P
pomozov2022-02-15 20:17:19
AJAX
pomozov, 2022-02-15 20:17:19

How to get request body in onload event?

I am writing a function to intercept all successful AJAX requests. But I can't figure out how to get the content of the request body. Tried like this, but it doesn't work.

(function(send) {
    XMLHttpRequest.prototype.send = function (request) { 
    this.addEventListener('load', function (request) {
      if (this.responseURL.indexOf(location.origin) == 0) {
        console.log(request);	        
          };
    });
    send.apply(this, arguments);
    }
})(XMLHttpRequest.prototype.send)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2022-02-15
@pomozov

var original = {
  open: XMLHttpRequest.prototype.open,
  send: XMLHttpRequest.prototype.send
};

XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
  console.log('Параметры запроса:', [method, url, async, user, password]);
  return original.open.call(this, method, url, async, user, password);
};

XMLHttpRequest.prototype.send = function (data) {
  console.log('Тело запроса:', data);
  return original.send.call(this, data);
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question