How to get responsecode from store callback?

2020-02-12 10:27发布

I'm trying to handle session timeout server-side. When getting session timeout, my server sends back a response with json {success: false}, ContentType: 'application/json', ResponseNo: 408

store:

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' }
  }
});

And on the client side, I handle callback loading store like this:

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

To perform different responses, I'd like to change alert. So, if response no. is 408, I can alert session expired (and so on, managing response numbers).

But I didn't find any way to get response no. in store callback!

Any suggestions?

5条回答
Melony?
2楼-- · 2020-02-12 10:49

I know this question is pretty "old", but in ExtJS 4.2.2, when

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

happens, you can automatically catch the server response using

b.error.status

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

I don't know if it was implemented only a few time ago (after this was posted I mean), but still it can help anyone checking this post in the future I guess...

查看更多
叛逆
3楼-- · 2020-02-12 10:53

Try this (on extjs 4.2.2)

callback: function (records, operation, success) {
            operation.request.operation.response.status; }
查看更多
4楼-- · 2020-02-12 10:54

Unfortunately, the callback method does not have the server response passed in as a parameter. This is likely since there are many ways to load data into a store, and not all of them will have a server response.

You can override the proxy's processResponse function to store the server's response with the operation object, then access it in your callback.

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);
   }
});

Then, to get the status:

storeAssets.load({
   scope: this,
   callback: function(records, operation, success) {
      if (operation.serverResponse.status === 408) {
         Ext.Msg.alert('Session expired');
      }
   }
});
查看更多
ら.Afraid
5楼-- · 2020-02-12 10:55

I know this is quite old now, but I had a similar problem. The solution I found was to listen for the exception event in the proxy.

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

I also predicated my store load callback to only proceed when success is true. Hopefully someone else searching will find this helpful.

查看更多
疯言疯语
6楼-- · 2020-02-12 11:09

Solved adding following code:

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');
          }
        }
      }
});

When i call ajax request, server gives back information that are catched by this exception. Now I can handle callback code!

查看更多
登录 后发表回答