Waqar Haider
Waqar Haider

Reputation: 971

Adding exceptions on Ext.ajax requests Ext JS 6

I want to add 400, 500 exception for all the AJAX requests. Here is what I have done for beforeload on every AJAX request.

Ext.define('Inertia.view.BaseUrl', {
singleton : true,
constructor : function(config) {

    this.initConfig(config);
    Ext.Ajax.on('beforerequest', this.onBeforeRequest, this);
},

onBeforeRequest : function(connection, options) {
    options.headers = {
        'token': 'random-token',
        'code': 'random-codeABC'
    };
}

 });

Now I want to add an exception for all the requests, that if the request somehow didn't load I want to catch an exception. How would I do that I try this but it didn't work

Ext.define('Inertia.view.BaseUrl', {
singleton : true,
constructor : function(config) {

    this.initConfig(config);
    Ext.Ajax.on('beforerequest', this.onBeforeRequest, this);
    Ext.Ajax.on('exception', this.exception, this);
},

onBeforeRequest : function(connection, options) {
    options.headers = {
        'token': 'random-token',
        'code': 'random-codeABC'
    };
},
 exception: function (){
     alert('An Exception occured...!');
   }
 });

Upvotes: 1

Views: 947

Answers (1)

Rohit Sharma
Rohit Sharma

Reputation: 1410

You can use it like below:-

/**
 * @ajaxrequest error handling
 */
Ext.Ajax.on('requestexception', function (conn, response, options, eOpts) {
    try {
        alert(response.status);
    } catch (error) {
        console.log(error);
    }
});

Or like this:-

requestexception: function (conn, response, options, eOpts) {
    try {
        alert(response.status);
    } catch (error) {
        console.log(error);
    }
}

Upvotes: 2

Related Questions