extjs4 global network exception listener

泪湿孤枕 提交于 2019-12-14 00:33:09

问题


I want to write a listener that will listen to all network requests errors, something like this :

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

The above code works only for requests via Ext.Ajax.request(), how to rewrite it so it could work also for form submits, url not found error etc.

On server side I have Spring MVC that dispatches all requests and if there is any error, the response status of 555 is returned.

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);
     }
 });

回答1:


This should work:

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 ); 
    }
});

Have a look at the source code for the exact handleResponse() method from which I copied most of the code above.




回答2:


IMHO you don't need to override anything. You can place a listener on the Ext.Ajax singleton like it is described here:

Override Ext.data.Connection - Best Practice

Another option is to use the Ext.util.Observable.observe() function as described here:

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
});


来源:https://stackoverflow.com/questions/11626428/extjs4-global-network-exception-listener

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