json extjs 存储错误处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5141121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
extjs store error handling
提问by kalan
I am trying to handle an exception in an Ext.data.Storeinstance when creating a new Ext.data.Record. When the server responds with the following json:
我正在尝试Ext.data.Store在创建新的Ext.data.Record. 当服务器使用以下 json 响应时:
{"success": false, "message": "some text"}
I get an exception of type 'request', even though the server returns an HTTP 200 Response!
即使服务器返回 HTTP 200 响应,我也收到了“请求”类型的异常!
To get a 'remote' error I have to create an object with the rootproperty
要获得“远程”错误,我必须使用该root属性创建一个对象
({
"success": false,
"message": "some text",
"data": {
"PositionId": "00000000-0000-0000-0000-000000000000",
"Name": "123"
}
})
...but I don't want this. Is there any way to change this behaviour?
……但我不想要这个。有没有办法改变这种行为?
Also, when I insert a record in the store, it is automatically added to the associated grid, but if an error occurs it remains there, so I need to reload store on every error. Is there any better way to do this?
此外,当我在商店中插入一条记录时,它会自动添加到相关联的网格中,但如果发生错误,它会保留在那里,所以我需要在每个错误时重新加载存储。有没有更好的方法来做到这一点?
采纳答案by kalan
Finally, I've found out that if I send back empty data it works as expected. So I don't need to send back any fictional data, my server response is:
最后,我发现如果我发回空数据,它会按预期工作。所以我不需要发回任何虚构的数据,我的服务器响应是:
({
"success": false,
"message": "some text",
"data": {}
})
回答by Joseph Lust
You should catch one of the two Store events:
您应该捕获以下两个 Store 事件之一:
loadexception(deprecated)exception
loadexception(已弃用)exception
For example you could:
例如,您可以:
// 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();
You could also just use the successand failureevents from the store to take action based on the successflag.
您也可以仅使用商店中的成功和失败事件来根据成功标志采取行动。
回答by code4jhon
when success is false operation doesn't have a response property. This thread explains it very clairly!
当成功为假时,操作没有响应属性。这个线程解释得非常清楚!
http://www.sencha.com/forum/showthread.php?196013-access-operation.response-when-success-false
http://www.sencha.com/forum/showthread.php?196013-access-operation.response-when-success-false
Example:
例子:
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
});
Implementation:
执行:
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.');
}
}
}
});
Best regards @code4jhon
最好的问候@code4jhon

