0

I use a jQuery plugin named Redactor, which can send ajax requests (link to the code). My site has an ajax authentication using header. So, I need to set header of all ajax requests, sending via redactor.

How can I modify just this request of the website without modifying the source file?

P.S. I don't need to modify XmlHttpRequest globally. I want to modify it just for this chunk of code.

Alex Antonov
  • 14,134
  • 7
  • 65
  • 142

1 Answers1

1

You can use ajaxSetup() ( https://api.jquery.com/jquery.ajaxsetup/ ). You can define default values for all ajax request in jQuery. So you can force the header to requests sent by Redactor.

See this question : How can I add a custom HTTP header to ajax request with js or jQuery?

If you want to add a header (or set of headers) to every request then use the beforeSend hook with $.ajaxSetup():

$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('x-my-custom-header', 'some value');
    }
});

// Sends your custom header
$.ajax({ url: 'foo/bar' });

// Sends both custom headers
$.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } });
Community
  • 1
  • 1
Magus
  • 14,796
  • 3
  • 36
  • 51