ajax setup过滤机制,获取后台的运行时异常(RuntimeException)做提示信息

前端之家收集整理的这篇文章主要介绍了ajax setup过滤机制,获取后台的运行时异常(RuntimeException)做提示信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//运行是异常 ajaxSetup 是ajax的一个拦截器,可以拦截表单的提交,也可以拦截后台的运行时异常,做一些提示弹框 ajaxSetup只会补做运行时异常 利用ajax的setup控制将系统的错误信息弹出到页面 timeout.js $(function() { // 解决ajax超时问题 by wanghao 2013-10-31 begin var isChecked = false; var isCheckedValue = null; var isTimeout = false; //这个可以单独的放到另外一个js var showExceptionInfo = function(msg){ showWindow("exceptionWindow","错误提示"); $("#exceptionWindow .ball_layer_pointout06").text("系统服务异常,异常信息:"+msg); }; $.ajaxSetup({ "cache" : false,"complete" : function(XMLHttpRequest,textStatus) { // 在每个ajax请求后进行删除权限验证 if (XMLHttpRequest.responseText == "IS_CAN_DELETE_TRUE" || XMLHttpRequest.responseText == "IS_CAN_DELETE_FALSE") { // 如果本身已经获取删除权限,不再继续验证,从而避免死循环 return; } },"error" : function(textStatus,errorThrown) { if (textStatus.status == 200) { return; } unDisplayProgress(); // 全局处理异常,所有的业务代码都不再需要手动处理error!!!by wanghao 2013-12-3 var text = textStatus.responseText; var start = text.indexOf("<h1>"); var end = text.indexOf("</h1>"); var error = text.substring(start + 4,end); if (error == "TIME_OUT" && !isTimeout) { isTimeout = true; alert("操作超时,为保证系统安全,请重新登录,谢谢!"); window.location = basePath; return; } else if (!error) { // console.log("后台异常,但是无异常信息,不影响系统功能!"+text); } else { showExceptionInfo(error); } } }); var renderDeleteButton = function(isCanDelete) { // console.log(new Date() + "-->" +isCanDelete); if (isCanDelete == "IS_CAN_DELETE_TRUE") { $("link[title='extra']").attr("disabled","disabled"); $(".del_report_btn").css("display","inline-block"); $(".table_delete_btn").css("display","inline-block"); $(".del_btn").css("display","inline-block"); $(".table_delete_dis_btn").css("display","inline-block"); } else if (isCanDelete == "IS_CAN_DELETE_FALSE") { $("link[title='extra']").removeAttr("disabled"); $(".del_report_btn").remove(); $(".table_delete_btn").remove(); $(".del_btn").remove(); $(".table_delete_dis_btn").remove(); } }; // 立即执行删除权限验证 //checkIsCanDelete(); }); jsp: 这个错误消息框可以在simesh中配置,装饰到每一个页面 <div id="exceptionWindow" class="ball_Box ball_Box06" style="display:none;z-index:99999999;width:290px;"> <div class="bg_boder"> <h2> <a href="#" class="del"></a>错误提示 </h2> <div class="ball_layer_pointout06"></div> <div class="ball_btn_Box"> <center> <a href="javascript:void(0)" class="ball_btn_true" >确定</a> </center> </div> </div> </div> //后台处理时抛出运行是异常即可 @Service("overviewService") public class OverviewServiceImpl implements IOverviewService { private Logger logger = Logger.getLogger(OverviewServiceImpl.class); @Override public SummeryOfResourcesVO getSummeryOfResource() { logger.info("查询一组资源使用情况(云主机、虚拟内核、虚拟内存、安全组)信息"); final SummeryOfResourcesVO vo = new SummeryOfResourcesVO(); final JSONObject[] provider = new JSONObject[1]; final String[] reslt= new String[1]; /**取资源使用情况*/ HttpClientRequest request = new HttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP") + "/rest/resource_used"); try { request.process(new HttpClientResponseHandler() { @Override public void handle(String response,String retCode,String msg) throws HttpClientException,ManoException { logger.debug("查询一组资源使用情况信息,返回状态:" + retCode + ",返回信息:" + msg); JSONObject obj = JSONObject.parSEObject(response); if(obj.get("result")!=null){ System.out.println(obj.get("result").toString()); JSONObject jsonObj = JSONObject.parSEObject(obj.get("result").toString()); if (jsonObj == null){ //&&!reslt[0].equals("0") return; } // 一 、请求并计算 云主机信息 Integer vms = jsonObj.getInteger(Constants.OverView.VMS); Integer used_vms = jsonObj.getInteger(Constants.OverView.USED_VMS); Integer unused_vms = 0; if (vms == null || vms.equals("")) { vms = 0; } else if (used_vms == null || used_vms.equals("")) { used_vms = 0; } else { unused_vms = vms - used_vms; } vo.setCloudPlatformAll(String.valueOf(vms)); vo.setCloudPlatformUsed(String.valueOf(used_vms)); vo.setCloudPlatformUnUsed(String.valueOf(unused_vms)); // 二 、请求 虚拟内核 Integer cpus = jsonObj.getInteger(Constants.OverView.cpuS); Integer used_cpus = jsonObj.getInteger(Constants.OverView.USED_cpuS); Integer unused_cpus = 0; if (cpus == null || cpus.equals("")) { cpus = 0; } else if (used_cpus == null || used_cpus.equals("")) { used_cpus = 0; } else { unused_cpus = cpus - used_cpus; } vo.setVirtualKernelAll(String.valueOf(cpus)); vo.setVirtualKernelUsed(String.valueOf(used_cpus)); vo.setVirtualKernelUnUsed(String.valueOf(unused_cpus)); // 三、请求 虚拟内存 Integer mems = jsonObj.getInteger(Constants.OverView.MEMS); Integer used_mem = jsonObj.getInteger(Constants.OverView.USED_MEM); Integer unused_mem = 0; if (mems == null || mems.equals("")) { mems = 0; } else if (used_mem == null || used_mem.equals("")) { used_mem = 0; } else { unused_mem = mems - used_mem; } vo.setVirtualMemoryAll(String.valueOf(mems)); vo.setVirtualMemoryUsed(String.valueOf(used_mem)); vo.setVirtualMemoryUnUsed(String.valueOf(unused_mem)); // 四、请求 安全组 Integer security_group = jsonObj.getInteger(Constants.OverView.SECURITY_GROUP); Integer used_security_group = jsonObj.getInteger(Constants.OverView.USED_SECURITY_GROUP); Integer unused_security_group = 0; if (security_group == null || security_group.equals("")) { security_group = 0; } else if (used_security_group == null || used_security_group.equals("")) { used_security_group = 0; } else { unused_security_group = security_group - used_security_group; } vo.setSecurityGroupAll(String.valueOf(security_group)); vo.setSecurityGroupUsed(String.valueOf(used_security_group)); vo.setSecurityGroupUnUsed(String.valueOf(unused_security_group)); }else{ /**provider连接数*/ HttpClientRequest requestProvider = new HttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP") + "/rest/provider_network_cn"); try { requestProvider.process(new HttpClientResponseHandler() { @Override public void handle(String response,String msg) throws HttpClientException { logger.debug("查询一组资源使用情况信息,返回信息:" + msg); provider[0] = JSONObject.parSEObject(response); System.out.println(provider[0].get("result").toString()+"rrrrrrrrrrrrr"); String jsonObj = provider[0].get("result").toString(); reslt[0] =jsonObj; if (jsonObj == null) return; } }); } catch (HttpClientException e) { e.printStackTrace(); } //////////////////ManoException是运行是异常 if(reslt[0].equals("0")){ throw new ManoException("0","没建立与openstack的连接,请检查openstack资源中openstack提供者是否已添加"); }else{ throw new ManoException("1","没有数据"); } //throw new HttpClientException("没建立连接"); } }}); } catch (HttpClientException e) { throw new ManoException("1","没建立连接"); } logger.info("SummeryOfResourcesVO is " + vo.toString()); return vo; } } //运行时异常 package com.certusnet.nfv.mano.exception; import java.util.Locale; import javax.ws.rs.ext.Provider; import org.springframework.web.context.ContextLoaderListener; /** * ManoException,统一处理异常编码和异常信息 * @author Biwei * */ @Provider public class ManoException extends RuntimeException { public static final long serialVersionUID = 11111111111L; public ManoException(Throwable throwable) { super(throwable); } public ManoException(String code,String msg) { super(msg); this.code = code; } /** * message key */ private String code; /** * message params */ private Object[] params; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public Object[] getParams() { return params; } public void setParams(Object[] params) { this.params = params; } }

猜你在找的Ajax相关文章