判断是不是是ajax请求,区分返回页面值
1个回答
2017-03-28 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
判断是否是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) {
if (data['access-denied']) {
if (data.cause==='AUTHENTICATION_FAILURE') {
alert('登录超时,请重新登录.');
window.location.href = contextPath + '/';
} else if (data.cause==='AUTHORIZATION_FAILURE') {
if (data=="noright")
{
alert('对不起,你没有访问该资源的权限.');
}
}
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) {
if (data['access-denied']) {
if (data.cause==='AUTHENTICATION_FAILURE') {
alert('登录超时,请重新登录.');
window.location.href = contextPath + '/';
} else if (data.cause==='AUTHORIZATION_FAILURE') {
if (data=="noright")
{
alert('对不起,你没有访问该资源的权限.');
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |