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

Ext.data.Connection.override request

Varois
1#
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:

Ext.data.Connection.override({

    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.
2#
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:

Ext.Ajax.setWithCredentials(true);
Ext.Ajax.on({
    beforerequest: function(conn, options) {
        options.headers = options.headers || {};
        options.headers['Authorization'] = 'Bearer VAROIS1iOiJKV1QiLCJh';
    },
    requestexception: function(conn, response, options) {
        Ext.Error.raise('...');
    }
});

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

JChap
3#
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() {
        Ext.Ajax.on({
            beforerequest: function(conn, opts){
                if(opts.url && Ext.String.startsWith(opts.url, '/')) {
                    return;
                }
                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