我想写一个listener
,它将监听所有网络请求错误,类似于以下内容:
Ext.Ajax.on('requestexception', function(conn, response, options) {
if (response.status === 555) {
Ext.Msg.alert('test', 'test');
}
});
以上代码仅适用于通过Ext.Ajax.request()
的请求,如何重写它,使其也适用于表单提交、url未找到错误等。
在服务器端,我有SpringMVC,它调度所有请求,如果有任何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);
}
});
这应该有效:
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 );
}
});
看看handleResponse()
方法的源代码,我从中复制了上面的大部分代码。
IMHO您不需要覆盖任何内容。您可以在Ext.Ajaxsingleton上放置一个监听器,如下所述:
覆盖Ext.data.Connection-最佳实践
另一种选择是使用Ext.util.ObsObservable.obsObserve()函数,如下所述:
http://www.sencha.com/forum/showthread.php?172269-用于500-404-403响应代码的全局连接处理程序
Ext.util.Observable.observe(Ext.data.Connection);
Ext.data.Connection.on('requestexception', function(conn, response, options, eOpts) {
//...handle it
});