Alex Antonov
Alex Antonov

Reputation: 15176

Modify XmlHttpRequest just inside one function of the code

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.

Upvotes: 0

Views: 152

Answers (1)

Magus
Magus

Reputation: 15124

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' } });

Upvotes: 1

Related Questions