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