日期:2014-05-16  浏览次数:20676 次

判断是否是ajax请求,区分返回页面值
boolean isAjax = "XMLHttpRequest".equals(req.getHeader("X-Requested-With")); 
//如果是ajax请求 
        if (isAjax) {        
             
            String jsonObject = "{\"message\":\"You are not privileged to request this resource.\",\"access-denied\":true,\"cause\":\"AUTHORIZATION_FAILURE\"}"; 
            String contentType = "application/json"; 
            resp.setContentType(contentType); 
            PrintWriter out = resp.getWriter(); 
            out.print(jsonObject); 
            out.flush(); 
            out.close(); 
            return; 
        } 
        else 
        { 
         
         String path = req.getContextPath(); 
         String basePath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/"; 
         resp.sendRedirect(basePath+accessDeniedUrl); 
        } 
//全局设置 jquery_ajax事件
(function($){ 
    // 保存原有的jquery ajax; 
    var $_ajax = $.ajax; 
     
    $.ajax = function(options){ 
        var originalSuccess, 
            mySuccess, 
            success_context; 
     
        if (options.success) { 
                        // save reference to original success callback 
            originalSuccess = options.success; 
            success_context = options.context ? options.context : $; 
             
                        // 自定义callback 
            mySuccess = function(data) {