How to get response message from server on Store.sync()

北城余情 提交于 2021-01-28 06:27:04

问题


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 the result.message if filled by the proxy's reader, using the reader's messageProperty

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!