Ext JS 4 - Ajax和Rest代理处理服务器端异常和消息的方法
2016-05-12 17:21
495 查看
<span style="color: rgb(102, 102, 102); font-family: 微软雅黑, PMingLiU, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 22px; background-color: rgb(244, 247, 231);">可能要处理的情况:</span><br style="color: rgb(102, 102, 102); font-family: 微软雅黑, PMingLiU, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 22px; background-color: rgb(244, 247, 231);" /><span style="color: rgb(102, 102, 102); font-family: 微软雅黑, PMingLiU, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 22px; background-color: rgb(244, 247, 231);">success(成功)——Ext处理</span><br style="color: rgb(102, 102, 102); font-family: 微软雅黑, PMingLiU, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 22px; background-color: rgb(244, 247, 231);" /><span style="color: rgb(102, 102, 102); font-family: 微软雅黑, PMingLiU, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 22px; background-color: rgb(244, 247, 231);">failure(失败),由于通讯问题——Ext处理</span><br style="color: rgb(102, 102, 102); font-family: 微软雅黑, PMingLiU, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 22px; background-color: rgb(244, 247, 231);" /><span style="color: rgb(102, 102, 102); font-family: 微软雅黑, PMingLiU, Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 22px; background-color: rgb(244, 247, 231);">failure(失败),由于服务器端异常——开发人员人员必须处理的响应失败……</span>
<strong>方案一</strong>
//Ajax Response Error HandlerExt.Ajax.on('requestexception', function(conn, response, options, eOpts) {var error = response.status + ' - ' + response.statusText;console.log('Ajax Request Exception! '+error);if (response.status != 200) {var errorData = Ext.JSON.decode(response.responseText); console.log('ajax req error:'+errorData.message);console.log('Ajax request Error', response.status);}});
<strong>方案二</strong>
<pre name="code" class="javascript">store.on('loadexception',<span style="white-space:pre"> </span>function(a,conn,resp) {<span style="white-space:pre"> </span>if (resp.status == '304') {<span style="white-space:pre"> </span>Ext.Msg.alert('Content has not changed');<span style="white-space:pre"> </span>}else if(resp.status == '200') {<span style="white-space:pre"> </span>return; //Do nothing<span style="white-space:pre"> </span>}else if (resp.status == '401') {<span style="white-space:pre"> </span>Ext.Msg.alert('Authentication required - You need to Login');<span style="white-space:pre"> </span>}else if (resp.status == '302') {<span style="white-space:pre"> </span>errorDialog.body.update('Session Has Expired');<span style="white-space:pre"> </span>errorDialog.show();<span style="white-space:pre"> </span>}else if(resp.status == '500') {<span style="white-space:pre"> </span>errorDialog.body.update(resp.responseText);<span style="white-space:pre"> </span>errorDialog.show();<span style="white-space:pre"> </span>}else{<span style="white-space:pre"> </span>errorDialog.body.update('An uncaught exception has occured');<span style="white-space:pre"> </span>errorDialog.show();<span style="white-space:pre"> </span>}}<strong></strong>
<strong>方案三</strong>
function requestMessageProcessor(proxy, response) { if (response && proxy) { try { var responseData = proxy.reader.getResponseData(response); if (responseData.message) { var messageDescription = 'Information'; // title of the alert box var messageIcon = Ext.MessageBox.INFO; if (!responseData.success) { var messageDescription = 'Error'; var messageIcon = Ext.MessageBox.ERROR; } Ext.MessageBox.show({ title: messageDescription, msg: responseData.message, buttons: Ext.MessageBox.OK, icon: messageIcon }); } } catch(err) { // Malformed response most likely console.log(err); } }}And here’s the part which should reside in proxy:proxy: { ... listeners: { exception: function(proxy, response, options) { requestMessageProcessor(proxy, response); } }, afterRequest: function(request, success) { requestMessageProcessor(request.scope, request.operation.response); }}
相关文章推荐
- c# 执行javascript
- js自定义select下拉框美化特效
- Jstorm-------storm.yaml配置
- 一个问题提交的实例(js原生动画,原生ajax,js引用加参数)
- JavaScript
- jsonp跨域调用
- 利用jsp+uploadify插件实现删除上传到ftp服务器里面的文件
- phantomjs使用说明
- JS函数的定义与调用方法推荐
- 图解JSP与Servlet的关系
- JSP应用导出Excel报表的简单实现以及中文乱码彻底解决(HTML)
- pyspider 爬虫教程(三):使用 PhantomJS 渲染带 JS 的页面
- 2016.5.12 JavaScript笔记
- servlet jsp 经典总结
- JavaScript性能优化技巧之函数节流
- JS定义类的六种方式详解
- 程序源弃坑之路7(js软键盘)
- My97DatePicker日期插件WdatePicker.js的使用方法
- 面试-javascript知识
- ExtJS中DragDrop插件的一些使用实例