可能要处理的情况:
success(成功)——Ext处理
failure(失败),由于通讯问题——Ext处理
failure(失败),由于服务器端异常——开发人员人员必须处理的响应失败……
- //AjaxResponseErrorHandler@H_404_22@@H_404_22@@H_404_22@
- Ext.Ajax.on('requestexception'@H_404_22@,@H_404_22@function@H_404_22@(conn,response,options,eOpts){@H_404_22@@H_404_22@
- var@H_404_22@error=response.status+@H_404_22@'-'@H_404_22@+response.statusText;@H_404_22@@H_404_22@
- console.log('AjaxRequestException!'@H_404_22@+error);@H_404_22@@H_404_22@
- if@H_404_22@(response.status!=200){@H_404_22@@H_404_22@
- arerrorData=Ext.JSON.decode(response.responseText);console.log('ajaxreqerror:'@H_404_22@+errorData.message);@H_404_22@@H_404_22@
- console.log('AjaxrequestError'@H_404_22@,response.status);@H_404_22@@H_404_22@
- }@H_404_22@
- });@H_404_22@
解决方案二:
当在服务器端发生异常时,可以将500作为响应标头,原因作为HTML内容发送回客户端。
- store.on(@H_404_22@'loadexception'@H_404_22@,@H_404_22@@H_404_22@
- function@H_404_22@(a,conn,resp){@H_404_22@@H_404_22@
- if@H_404_22@(resp.status==@H_404_22@'304'@H_404_22@){@H_404_22@@H_404_22@
- Ext.Msg.alert('Contenthasnotchanged'@H_404_22@);@H_404_22@@H_404_22@
- }else@H_404_22@@H_404_22@if@H_404_22@(resp.status==@H_404_22@'200'@H_404_22@){@H_404_22@@H_404_22@
- return@H_404_22@;@H_404_22@//Donothing@H_404_22@@H_404_22@@H_404_22@
- }else@H_404_22@@H_404_22@if@H_404_22@(resp.status==@H_404_22@'401'@H_404_22@){@H_404_22@@H_404_22@
- Ext.Msg.alert('Authenticationrequired-YouneedtoLogin'@H_404_22@);@H_404_22@@H_404_22@
- }else@H_404_22@@H_404_22@if@H_404_22@(resp.status==@H_404_22@'302'@H_404_22@){@H_404_22@@H_404_22@
- errorDialog.body.update('SessionHasExpired'@H_404_22@);@H_404_22@@H_404_22@
- errorDialog.show();@H_404_22@
- }else@H_404_22@@H_404_22@if@H_404_22@(resp.status==@H_404_22@'500'@H_404_22@){@H_404_22@@H_404_22@
- errorDialog.body.update(resp.responseText);@H_404_22@
- errorDialog.show();@H_404_22@
- }else@H_404_22@{@H_404_22@@H_404_22@
- errorDialog.body.update('Anuncaughtexceptionhasoccured'@H_404_22@);@H_404_22@@H_404_22@
- errorDialog.show();@H_404_22@
- }@H_404_22@
- }@H_404_22@
解决方案三:
当发送Ajax或REST请求时,Ext JS 4代理通常会预期返回的信息包括参数:data、success和message。参数message是可选的,不过当需要将请求结果显示给用户的时候,它就可派上用场了。
- function@H_404_22@requestMessageProcessor(proxy,response){@H_404_22@@H_404_22@
- if@H_404_22@(response&&proxy){@H_404_22@@H_404_22@
- try@H_404_22@{@H_404_22@@H_404_22@
- var@H_404_22@responseData=proxy.reader.getResponseData(response);@H_404_22@@H_404_22@
- @H_404_22@
- if@H_404_22@(responseData.message){@H_404_22@@H_404_22@
- var@H_404_22@messageDescription=@H_404_22@'Information'@H_404_22@;@H_404_22@//titleofthealertBox@H_404_22@@H_404_22@@H_404_22@
- var@H_404_22@messageIcon=Ext.MessageBox.INFO;@H_404_22@@H_404_22@
- @H_404_22@
- if@H_404_22@(!responseData.success)@H_404_22@@H_404_22@
- {@H_404_22@
- var@H_404_22@messageDescription=@H_404_22@'Error'@H_404_22@;@H_404_22@@H_404_22@
- var@H_404_22@messageIcon=Ext.MessageBox.ERROR;@H_404_22@@H_404_22@
- }@H_404_22@
- @H_404_22@
- Ext.MessageBox.show({@H_404_22@
- title:messageDescription,@H_404_22@
- msg:responseData.message,@H_404_22@
- buttons:Ext.MessageBox.OK,@H_404_22@
- icon:messageIcon@H_404_22@
- });@H_404_22@
- }@H_404_22@
- }@H_404_22@
- catch@H_404_22@(err){@H_404_22@@H_404_22@
- //Malformedresponsemostlikely@H_404_22@@H_404_22@@H_404_22@
- console.log(err);@H_404_22@
- }@H_404_22@
- }@H_404_22@
- }@H_404_22@
- Andhere’sthepartwhichshouldresidein@H_404_22@proxy:@H_404_22@@H_404_22@
- @H_404_22@
- proxy:{@H_404_22@
- ...@H_404_22@
- listeners:{@H_404_22@
- exception:function@H_404_22@(proxy,options){@H_404_22@@H_404_22@
- requestMessageProcessor(proxy,response);@H_404_22@
- }@H_404_22@
- },@H_404_22@
- afterRequest:function@H_404_22@(request,success){@H_404_22@@H_404_22@
- requestMessageProcessor(request.scope,request.operation.response);@H_404_22@
- }@H_404_22@
- } @H_404_22@