// 处理Ajax请求 HttpServletResponse response = (HttpServletResponse) arg1; if ("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))) { response.setCharacterEncoding("UTF-8"); response.getWriter().write("{\"status\": 302,\"msg\": \"请先登录!\"}"); } else { response.sendRedirect(request.getContextPath() + "/index.jsp"); }
前端处理(Ajax全局设置):
$.ajaxSetup({ complete: function(r,status) { var json = JSON.parse(r.responseText); if(json.status == 302){ top.location.href = "${pageContext.request.contextPath }/"; } } });原文链接:https://www.f2er.com/ajax/161071.html