问题
I have a store which I'd like to sync()
with server.
Store.sync()
method have success
and failure
property-functions, they have Ext.data.Batch
and options
as parameters.
If I get such response from server:
{
success: false,
msg: "test error!"
}
failure
method invokes.
How to access response msg
property from within failure
method?
回答1:
store.sync({
failure: function(batch, options) {
alert(batch.proxy.getReader().jsonData.msg);
}
});
回答2:
Store.sync() method have success and failure property-functions, they have Ext.data.Batch and options as parameters.
Any failed operations collecting inside batch.exceptions
array. It has all what you need.
Just go through the failed operations, processing them.
Operation failure message ("test error!") would be inside - operation.error
store.sync({
failure: function(batch, options) {
Ext.each(batch.exceptions, function(operation) {
if (operation.hasException()) {
Ext.log.warn('error message: ' + operation.error);
}
});
}
});
Proxy set it inside processResponse method:
operation.setException(result.message);
Where:
- Operation's setException method sets its
error
property (error message there). result
is Ext.data.ResultSet and theresult.message
if filled by the proxy's reader, using the reader'smessageProperty
But, by default reader's message property is messageProperty: 'message'
.
In your case you should configured the reader with correct messageProperty, like:
reader: {
...
messageProperty: 'msg'
}
or return from the server response with metadata
configuration property, like:
{
"success": false,
"msg": "test error!",
"metaData": {
...
"messageProperty": 'msg'
}
}
Json reader - Response MetaData (docs section)
来源:https://stackoverflow.com/questions/16035501/how-to-get-response-message-from-server-on-store-sync