【问题标题】:How to get responsecode from store callback?如何从商店回调中获取响应码?
【发布时间】:2013-05-22 14:09:12
【问题描述】:

我正在尝试在服务器端处理会话超时。 当会话超时时,我的服务器用 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(等等,管理响应号)。

但我没有找到任何方法来获得回复。店内回调!

有什么建议吗?

【问题讨论】:

  • 是的,你是对的,我删除了我的答案,并投票赞成你的问题..

标签: extjs extjs4.2


【解决方案1】:

很遗憾,回调方法没有将服务器响应作为参数传入。这可能是因为有很多方法可以将数据加载到存储中,但并非所有方法都会有服务器响应。

您可以覆盖代理的 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');
      }
   }
});

【讨论】:

    【解决方案2】:

    我知道这已经很老了,但我遇到了类似的问题。 我找到的解决方案是在代理中监听异常事件。

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

    我还预测我的商店加载回调仅在成功为真时才继续。 希望其他搜索的人会发现这很有帮助。

    【讨论】:

      【解决方案3】:

      试试这个(在 extjs 4.2.2 上)

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

      【讨论】:

        【解决方案4】:

        已解决添加以下代码:

        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 请求时,服务器会返回由此异常捕获的信息。现在我可以处理回调代码了!

        【讨论】:

          【解决方案5】:

          我知道这个问题很“老”,但在 ExtJS 4.2.2 中,当

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

          发生,您可以使用自动捕获服务器响应

          b.error.status

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

          我不知道它是否是在前一段时间才实施的(我的意思是在发布之后),但我想它仍然可以帮助任何人在未来查看这篇文章......

          【讨论】:

            猜你喜欢
            • 2020-06-19
            • 2020-08-18
            • 2022-11-25
            • 1970-01-01
            • 1970-01-01
            • 2013-01-22
            • 1970-01-01
            • 2018-11-30
            • 1970-01-01
            相关资源
            最近更新 更多