【问题标题】:extjs store error handlingextjs 存储错误处理
【发布时间】:2011-07-05 16:42:05
【问题描述】:

我试图在创建新的Ext.data.Record 时处理Ext.data.Store 实例中的异常。当服务器响应以下 json 时:

{"success": false, "message": "some text"}

即使服务器返回 HTTP 200 响应,我也会收到“请求”类型的异常!

要获得“远程”错误,我必须使用 root 属性创建一个对象

({
    "success": false,
    "message": "some text",
    "data": {
        "PositionId": "00000000-0000-0000-0000-000000000000",
        "Name": "123"
    }
})

...但我不想要这个。有什么办法可以改变这种行为吗?

另外,当我在 store 中插入一条记录时,它会自动添加到关联的网格中,但如果发生错误,它仍然存在,所以我需要在每个错误时重新加载 store。有没有更好的方法来做到这一点?

【问题讨论】:

  • 你能在你的问题上做一点吗?例如,您从哪里得到您看到的“请求”或“远程”错误?如果它在商店的“loadexception”监听器中,那么它应该只有“response”和“remote”的异常类型。您在哪里看到“请求”错误?如果您能提供代码示例,那将更好地帮助您回答问题。

标签: json extjs error-handling jsonstore


【解决方案1】:

您应该捕获两个商店事件之一:

  1. loadexception(已弃用)
  2. exception

例如你可以:

// make the store
var myStore = new Ext.data.Store({...});
// catch loading exceptions
myStore.on('exception',function( store, records, options ){
    // do something about the record exception
},this);
// load store
myStore.load();

您也可以只使用商店中的 successfailure 事件来根据 success 标志采取行动。

【讨论】:

    【解决方案2】:

    最后,我发现如果我发回空数据,它会按预期工作。所以我不需要发回任何虚构的数据,我的服务器响应是:

    ({
        "success": false,
        "message": "some text",
        "data": {}
    })
    

    【讨论】:

    • 我能提供更多细节吗?我尝试了同样的方法,但仍然需要再次重新加载网格:(
    • 我认为当您在响应中没有 root 属性时,阅读器会失败并且加载事件不会触发。但是当您发送空结果事件“加载”触发器时,您可以像这样检查返回的 json(至少在 ext4.2 中):this.getProxy().getReader().rawData;
    • 当成功为假时,您无法访问操作对象的响应属性。请看sencha.com/forum/…
    【解决方案3】:

    当成功为假时,操作没有响应属性。这个帖子解释得很清楚!

    http://www.sencha.com/forum/showthread.php?196013-access-operation.response-when-success-false

    例子:

    Ext.define("SC.store.SegurosCancelacionStore", {
        extend: "Ext.data.Store",
        model: "SC.model.PersonaSeguro",
        proxy: {
            timeout: 90000,
            actionMethods: {
                read   : 'POST'
            },
            type: "ajax",
            url: "../SegurosFinsolCancelacionServlet",
            reader: {
                type: "json",
                root: "seguros",
                messageProperty : 'msjError' //without this, it doesn't work
            }
        },
        autoLoad: false
    });
    

    实施:

    storeSegurosCancelacion.load({
                    params: {
                        'sucursal':sucursal,
                        'persona': persona
                    },
                    callback:function(records, operation, success){
                        msg.hide();
                        if(success == true){
                            if(records.length == 0){
                             Ext.Msg.alert('Resultado', 'No se ha encontrado información');
                            }
                        }
                        if(success == false){
                            try{
                                 Ext.Msg.alert('Error', operation.getError()); // way more elegant than ussing rawData etc ...
                            }catch(e){
                                    Ext.Msg.alert('Error', 'Error  inesperado en el servidor.');
                            }
                        }
                    }
                });
    

    最好的问候 @code4jhon

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 2013-05-17
      • 2021-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多