13

How can I add a request header to an ExtJS ajax request?

I specifically want to add the header: accept-encoding to equal true.

egerardus
  • 11,316
  • 12
  • 80
  • 123
Robo
  • 261
  • 2
  • 5
  • 13
  • You question is very unclear. Perhaps you can be more descriptive as for what exactly you're trying to achieve? – Izhaki Aug 01 '12 at 20:45
  • @Izhaki I clarified it to my understanding of the question. Hopefully I didn't take too many liberties with it... – egerardus Aug 01 '12 at 20:55

4 Answers4

14

You can specify request headers like this:

Ext.Ajax.request({
    url: 'yourUrl',
    headers: {
        'accept-encoding': 'true'
    }
})​
John Rice
  • 1,737
  • 8
  • 14
  • does this works also for loading stores (as a config of load at the same level as params ?) – code4jhon Aug 22 '14 at 16:03
  • Hi @code4jhon, you want something like this to set headers on a stores proxy: Ext.getStore('YourStore').proxy.headers = {'accept-encoding': 'true'} – John Rice Aug 24 '14 at 05:31
  • yeah, added config when defining the store. (config in the proxy obviously) thanks works fine. – code4jhon Aug 25 '14 at 15:31
5

Have you tried the headers config in the Ajax request:

Ext.Ajax.request({
    url: 'someURL',
    headers: {
        'accept-encoding': true
    }
});
egerardus
  • 11,316
  • 12
  • 80
  • 123
5

If you want to add a header to all Extjs ajax requests:

Ext.Ajax.defaultHeaders = {
    'accept-encoding' : true
};
sproc
  • 93
  • 1
  • 5
3

With ExtJs 6. Ext.Ajax.defaultHeaders doesn't work. But using the following setter works

Ext.Ajax.setDefaultHeaders({
    'accept-encoding' : true
});
VyTre
  • 103
  • 3
  • 14