2013-05-22 26 views
4

我想處理會話超時服務器端。 當越來越會話超時,我的服務器發回用JSON響應 {success: false}, ContentType: 'application/json', ResponseNo: 408如何從商店回調獲取響應代碼?

店:

var storeAssets = Ext.create('Ext.data.Store', { 
    model : 'modCombo', 
    autoLoad : false, 
    proxy : { limitParam : undefined, 
    startParam : undefined, 
    paramName : undefined, 
    pageParam : undefined, 
    noCache : false, 
    type : 'ajax', 
    url : '/caricaAssets.json', 
    reader : { root : 'data' } 
    } 
}); 

而在客戶端,我處理回調裝載店這樣的:

storeAssets.load({ 
    scope: this, 
    callback: function(records, operation, success) { 
    if (!success) { Ext.Msg.alert('Error'); } 
    } 
}); 

爲了執行不同的迴應,我想更改警報。 所以,如果回覆沒有。是408,我可以提醒session expired(依此類推,管理回覆號碼)。

但我沒有找到任何方式得到答覆沒有。在商店回撥!

有什麼建議嗎?

+0

是的,你是正確的,我刪除了我的答案,並投票了你的問題.. –

回答

4

不幸的是,回調方法沒有傳入作爲參數的服務器響應。這可能是因爲有很多方法可以將數據加載到商店中,而不是所有方法都會有服務器響應。

您可以覆蓋代理的processResponse函數來存儲服務器的響應與操作對象,然後在您的回調中訪問它。

Ext.define('Ext.data.proxy.ServerOverride', { 
    override: 'Ext.data.proxy.Server', 

    processResponse: function (success, operation, request, response, callback, scope) { 
     operation.serverResponse = response; 
     this.callParent(arguments); 
    } 
}); 

然後,要獲得狀態:

storeAssets.load({ 
    scope: this, 
    callback: function(records, operation, success) { 
     if (operation.serverResponse.status === 408) { 
     Ext.Msg.alert('Session expired'); 
     } 
    } 
}); 
0

解決添加以下代碼:

Ext.Ajax.on('requestexception', function(con, resp, op, e){ 
    if (resp.status === 408) { 
    Ext.Msg.alert('Warning', 'Session expired'); 
    } else { 
    if (resp.status === 404) { 
     Ext.Msg.alert('Error', Ext.util.Format.htmlEncode('Server not ready')); 
    } else { 
     if (resp.status !== undefined) { 
      Ext.Msg.alert('Error', Ext.util.Format.htmlEncode('Server not found (') + resp.status + ')'); 
     } else { 
      Ext.Msg.alert('Error', 'Server not found'); 
      } 
     } 
     } 
}); 

當我打電話Ajax請求,服務器還給受此例外逮住信息。現在我可以處理回調代碼了!

3

我知道這已經很老了,但我有類似的問題。 我找到的解決方案是偵聽代理中的異常事件。

proxy{ 
    type: 'ajax', 
    reader: { 
     type: 'json' 
    , 
    listeners: { 
     exception: function(proxy, response, options){ 
      Ext.MessageBox.alert('Error', response.status + ": " + response.statusText); 
     } 
    } 
} 

我還預測我的商店負載回調只有在成功時纔會執行。 希望有人搜索會發現這有幫助。

0

我知道這個問題是很 「老」,但在ExtJS的4.2.2,當

callback: function(a,b,c) {} 

情況時,可以自動使用搭上

b.error.status服務器響應

if (!c) { 
    if (b.error.status === 401) { 
     //logout 
    } 
} 

我不知道,如果它只是實現了幾個時間前(這個被張貼後,我的意思),但它仍然可以幫助任何人檢查這篇文章在日未來我猜...

1

試試這個(在extjs 4.2。2)

callback: function (records, operation, success) { 
      operation.request.operation.response.status; } 
相關問題