Ext.data.HttpProxy回调失败

时间:2011-03-30 18:27:36

标签: extjs

我有以下ExtJS。当响应成功时,将调用侦听器“write”(响应为JSON,如:{“success”:true,“message”:“......”})。但是,当响应不成功时,如何附加回调? ({ “成功”:假, “消息”: “......”})

tableStructure.proxy = new Ext.data.HttpProxy({
        api: {
            read: '/controller/tables/' + screenName + '/getstructure/' + table,
            create: '/controller/tables/' + screenName + '/createcolumn/' + table,
            update: '/controller/tables/' + screenName + '/updatecolumn/' + table,
            destroy: '/controller/tables/' + screenName + '/destroycolumn/' + table
        },

        listeners: {
            write: tableStructure.onWrite
        }
    });

3 个答案:

答案 0 :(得分:4)

您想要捕获HttpProxy的exception事件。

    listeners: {
        write: tableStructure.onWrite
        exception: function(proxy, type, action, options, response, arg) {
            if(type === 'remote') { // success is false
                // do your error handling here
                console.log( response ); // the response object sent from the server
            }
        }
    }

您可以在事件部分的Ext.data.HttpProxy的Ext文档中找到完整的文档。

答案 1 :(得分:0)

您应该能够使用write事件本身。写事件的签名是: write(dataproxy,action,data,response,record,options)

您可以从操作对象访问成功变量,并检查该值是true还是false。您应该能够访问成功变量:

action.result.success

你可以这样做:

if(action.result.success != true ) {
  // If success is not true
} else {
  // If success is true
}

答案 2 :(得分:0)

如果您发送的响应代码不是200,则还可以在HttpProxyexception handler设置Ext.data.Store

    var store = new CQ.Ext.data.Store({
        proxy : new CQ.Ext.data.HttpProxy({
            method : "GET",
            url : '/some_url'
        }),
        reader : new CQ.Ext.data.JsonReader(),
        baseParams : {
            param : 'some value'
        }
    });

    store.on("beforeload", function() {
        CQ.Ext.getBody().mask("Please wait...", false);
    });

    store.on("exception", function() {
        CQ.Ext.getBody().unmask();
        CQ.Ext.Msg.show({
            title: 'Error',
            msg: '<span style="color:red">Bad request.</span><br/>',
            icon: CQ.Ext.Msg.ERROR,
            buttons: CQ.Ext.Msg.OK
        });
    });