Sencha touch – manage ajax error

I’m developing a sencha touch app and I need to manage errors of every ajax call! The most simple solution consist of subscribe to every call and manage response but it’s very repetitive and I don’t like it!

The solution I choose is to extend base class of Connection and override response method.

In the launch method of app.js I added this code:

Ext.define('', {
override: '',

onComplete : function(request) {
var response = this.callParent([request]);

if(response.status != 200){
         var extraMsg = 'response code: ' + response.status;
         Ext.callback(Ext.goToLogin, this, [extraMsg]);
       }else if(response.responseText){
           //parse json resposne
           var resObj = Ext.JSON.decode(response.responseText);
           if(resObj && !resObj.success &&
             resObj.code === Ext.errorCodes.NOT_LOGGED){
             Ext.callback(Ext.goToLogin, this, ['Not logged!']););
   return response;

With this code every ajax call will be verified automatically.

In case of you want avoid this behavior for some request it will enought add ‘disableLoginRedirect=true’ in the request options!

  url: requestUrl,
  disableLoginRedirect : true, //this options!!!!
  failure: function(response, opts) {

If you are using JSONP the class to modify is:

Ext.define('', {
    override: '',

    createRequestCallback: function(request, operation, callback, scope) {
        var jsonPme = this;
        return function(success, response, errorType) {
            delete jsonPme.lastRequest;
            //console.log('jsonp callback');

            if(success && response){                  
                if(!response.success && response.code === Ext.errorCodes.NOT_LOGGED && (!request.options || !request.options.disableLoginRedirect)){
                    var extraMsg = 'not logged!';
                    Ext.callback(Ext.goToLogin, this, [extraMsg]);
                    jsonPme.processResponse(success, operation, request, response, callback, scope);
Tagged with: , , , , , , , , , , ,
Pubblicato su Ext, ExtJs, Sencha
2 comments on “Sencha touch – manage ajax error
  1. giulioroggero ha detto:

    Great post Carlo!

  2. […] via Sencha touch – manage ajax error. […]


Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:


Stai commentando usando il tuo account Chiudi sessione /  Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: