Home Ext.data.Connection.override request
Reply: 2

Ext.data.Connection.override request

Varois Published in 2018-01-12 20:09:15Z

I am trying to intercept all requests, before send, from an Extjs App, POST and GET and then manipulate the URL and add a new Header, Authorization: Bearer XYZ123.

I've this code:


    request: function (options) {
        var me = this;

        this.cors = true;
        this.useDefaultHeader = false;
        this.useDefaultXhrHeader = false;

        options.headers = { "Authorization": "Bearer VAROIS1iOiJKV1QiLCJh" };

        var separator = options.url.indexOf('/') == 0 ? "" : "/";
        options.url = UrlAPIPrefix + separator + options.url;

        return me.callOverridden(arguments);

But when i try to use it, the Header Authorization is not sent in both cases, GET and POST. Parameters are not sent when using POST. I am able to see params if debugging the code into Extjs files, but can't see it on chrome Request Headers, see image.

If any one knows how to do it, in one place, i will be glad if you can help me.

Benjamin E.
Benjamin E. Reply to 2018-01-14 11:37:03Z

That's because Ext.Ajax - the singleton of Ext.data.Connection - is used for most requests in the framework and overriding the latter does not affect the former. This overriding behavior was changed in extjs 5 and is intended by Sencha.

Use what you need of this instead:

    beforerequest: function(conn, options) {
        options.headers = options.headers || {};
        options.headers['Authorization'] = 'Bearer VAROIS1iOiJKV1QiLCJh';
    requestexception: function(conn, response, options) {

And don't hardcode the token into your javascript!!!

JChap Reply to 2018-01-12 20:25:57Z

Instead of overriding the class, i created a bootstrap class to set up the ajax interceptor. Called Bootstrap.init() in Application.launch() method.

Ext.define('MyApp.Bootstrap', {

    singleton : true,

    init: function() {
            beforerequest: function(conn, opts){
                if(opts.url && Ext.String.startsWith(opts.url, '/')) {
                if(opts.url && !Ext.String.startsWith(opts.url, MyApp.Config.SERVER_CONTEXT)) {
                    opts.url = MyApp.Config.SERVER_CONTEXT + opts.url;
                var clientId = MyApp.getApplication().clientId;
                opts.headers = Ext.apply({'X-Client-Id': clientId}, opts.headers);
                Ext.log('Sending Request to : ', opts.url);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.295644 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO