extjs4全局网络异常监听器

时间:2012-07-24 07:50:55

标签: extjs4.1 extjs-mvc

我想编写一个listener来监听所​​有网络请求错误,如下所示:

Ext.Ajax.on('requestexception', function(conn, response, options) {
    if (response.status === 555) {
        Ext.Msg.alert('test', 'test');
    }
});

以上代码仅适用于通过Ext.Ajax.request()的请求,如何重写它以便它也适用于表单提交,url未找到错误等。

在服务器端,我有Spring MVC调度所有请求,如果有error,则返回555的响应状态。

form.submit({
     url: dispatcher.getUrl('savePlanRequest'),
     //headers: {'Content-Type':'multipart/form-data; accept-charset=utf-8'},
     scope: this,
     method: 'GET',
     params: {
         scan: scan_id,
         attachments: attachments_id,
         parcels: parcels_id
     },
     success: function(form, action) {
         this.fireEvent('plansaved', this);
         Ext.Msg.alert(i18n.getMsg('success'), i18n.getMsg('gsip.view.plans.NewPlanForm.success_info'))
     },
     failure: function(form, action) {
         console.log('failure');
         //Ext.Msg.alert(i18n.getMsg('failure'), action.result.msg);
     }
 });

2 个答案:

答案 0 :(得分:2)

这应该有效:

Ext.override( Ext.form.action.Submit, { 
    handleResponse : function( response ) {

        var form = this.form,
            errorReader = form.errorReader,
            rs, errors, i, len, records;

        if (errorReader) {
             rs = errorReader.read(response);
             success = rs.success;
             // Do something if success is false
        }

        this.callParent ( arguments ); 
    }
});

请查看the source code,了解我复制上述大部分代码的确切handleResponse()方法。

答案 1 :(得分:1)

恕我直言,你不需要覆盖任何东西。您可以在Ext.Ajax单例上放置一个侦听器,如下所述:

Override Ext.data.Connection - Best Practice

另一种选择是使用Ext.util.Observable.observe()函数,如下所述:

http://www.sencha.com/forum/showthread.php?172269-Global-connection-handler-for-500-404-403-response-codes

Ext.util.Observable.observe(Ext.data.Connection);

Ext.data.Connection.on('requestexception', function(conn, response, options, eOpts) {
    //...handle it
});