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


Ext.Ajax.setDefaultHeaders({
            'Accept':'application/json'
        });

      



+4


source


how about overriding Ext.data.proxy.Ajax like this



Ext.override(Ext.data.proxy.Ajax, {
    headers: {
        token: 'xyz' // this token that every proxy should contain to communicate with your remote server. 
    }
});

      

0


source


Ext.Ajax is singleton, so you have to do this:

Ext.Ajax.defaultHeaders = {
  token: 'xyz'
};

      

Place it at the start of your application or wherever you see fit, and then every subsequent Ajax request will have this token.

0


source


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







All Articles