How to set common request headers for every ajax request in ext5?
I need to set up custom headers for each ajax request, is there a way to do this only once without manually configuring it in each ajax proxy.
proxy: {
headers: {
token: 'xyz' // this token that every proxy should contain to communicate with our remote server.
}
}
In jQuery, I can accomplish this using "ajaxPrefilter" like below:
jQuery.ajaxPrefilter(function(options, originalOptions, jqXHR) {
jqXHR.setRequestHeader('token', 'xyz');
}
But I don't know how to do this in extjs, please help!
+3
source to share
4 answers
Below code adds header and CSRF token for every ajax request made. Beforrequest note
Ext.Ajax.on('beforerequest', function(conn, options) {
var x = document.getElementsByTagName("META");
var token = "";
var headerVal = "";
var i;
for (i = 0; i < x.length; i++) {
if (x[i].name == "_csrf")
{
token = x[i].content;
}else if (x[i].name=="_csrf_header"){
headerVal = x[i].content;
}
}
//Ext.Ajax.defaultHeaders = Ext.apply(Ext.Ajax.defaultHeaders || {}, { headerVal : token });
Ext.Ajax.setDefaultHeaders({
headerVal : token
});
});
0
source to share