2012-09-09 51 views
0

在我的Sencha Touch應用程序中,如果設備連接到wifi但沒有互聯網連接,則Ext.device.Connection.isOnline()將返回true,因此我的Ajax調用掛起。我試圖捕捉requestexception事件,以便我可以優雅地處理錯誤,但無法使其正常工作。下面的代碼:如何在Sencha Touch 2中的Ajax請求中捕獲requestexception?

Ext.Ajax.request({ 
       url: [my url], 
       jsonData:{ }, 
       params:{ 
        op:  'myop' 

       }, 

       method:"POST", 
       success:function(response, opts){ 
        // all good 

       }, 
       failure:function(response, opts){ 
        // failed 
       }, 

       listeners: { 
        requestexception: function(connection, response, options, eOpts) { 


         switch(response.status) { 
          case 0 : 
           Ext.Msg.alert('Error Loading', 'No Internet connection.'); 
           break; 

          default : 
           Ext.Msg.alert('Whoops!', 'An unexpected error has occurred.'); 
          } 

        } 
       } 


      }); 

不知道我做錯了!

感謝 馬特

回答

0

Ext.define('MyApp.store.MyJsonStore',

extend: 'Ext.data.Store',

config: { 
    storeId: 'MyJsonStore', 
    proxy: { 
     type: 'ajax', 
     reader: { 
      type: 'json' 
     } 
    } 
}, 

constructor: function() { 
    var me = this; 
    me.callParent(arguments); 
    me.getProxy().on([{ 
     fn: 'onAjaxproxyException', 
     event: 'exception', 
     scope: me 
    }]); 
}, 

onAjaxproxyException: function(server, response, operation, options) { 
      //Your code here 
} 

});