Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are several solutions depending on what you need...</p> <p>If you want to <strong>add a custom header (or set of headers) to an individual request</strong> then just add the <code>headers</code> property:</p> <pre><code>// Request with custom header $.ajax({ url: 'foo/bar', headers: { 'x-my-custom-header': 'some value' } }); </code></pre> <p>If you want to <strong>add a default header (or set of headers) to every request</strong> then use <code>$.ajaxSetup()</code>:</p> <pre><code>$.ajaxSetup({ headers: { 'x-my-custom-header': 'some value' } }); // Sends your custom header $.ajax({ url: 'foo/bar' }); // Overwrites the default header with a new header $.ajax({ url: 'foo/bar', headers: { 'x-some-other-header': 'some value' } }); </code></pre> <p>If you want to <strong>add a header (or set of headers) to every request</strong> then use the <code>beforeSend</code> hook with <code>$.ajaxSetup()</code>:</p> <pre><code>$.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' } }); </code></pre> <p><strong>Edit (more info):</strong> One thing to be aware of is that with <code>ajaxSetup</code> you can only define one set of default headers and you can only define one <code>beforeSend</code>. If you call <code>ajaxSetup</code> multiple times, only the last set of headers will be sent and only the last before-send callback will execute.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload