欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Ajax請(qǐng)求時(shí)無(wú)法重定向的問(wèn)題解決代碼詳解

 更新時(shí)間:2019年06月21日 10:09:44   作者:冬眠的山谷  
這篇文章主要介紹了Ajax請(qǐng)求時(shí)無(wú)法重定向的問(wèn)題解決代碼詳解,當(dāng)使用Ajax請(qǐng)求時(shí),如果后臺(tái)進(jìn)行重定向到其他頁(yè)面時(shí)是無(wú)法成功的,只能在瀏覽器地址欄輸入才能夠?qū)崿F(xiàn)重定向。下面我們來(lái)看看解決方法吧

前言

今天發(fā)現(xiàn),當(dāng)使用Ajax請(qǐng)求時(shí),如果后臺(tái)進(jìn)行重定向到其他頁(yè)面時(shí)是無(wú)法成功的,只能在瀏覽器地址欄輸入才能夠?qū)崿F(xiàn)重定向。

Ajax默認(rèn)就是不支持重定向的,它是局部刷新,不重新加載頁(yè)面。

需要實(shí)現(xiàn)的功能是,后臺(tái)網(wǎng)關(guān)攔截請(qǐng)求,看請(qǐng)求中是否存在token.如果不存在就跳轉(zhuǎn)到登錄頁(yè)面。因?yàn)榇蠖鄶?shù)請(qǐng)求都是使用Ajax.一開始發(fā)現(xiàn)無(wú)法進(jìn)行重定向,每次都是返回到Ajax的結(jié)果處理函數(shù)。最終的解決辦法如下,需要后臺(tái)和前端進(jìn)行處理。

后臺(tái):

/**
*功能描述
* @author lgj
* @Description 重定向工具類
* @date 2/27/19
*/
@Slf4j
public class RedirecUtil {
/**
*功能描述
* @author lgj
* @Description 重定向
* @date 2/27/19
* @param:
* @return:
*
*/
public static void redirect(RequestContext ctx, String redirectUrl){
try{
//如果是Ajax請(qǐng)求
if("XMLHttpRequest".equals(ctx.getRequest().getHeader("X-Requested-With"))){
log.debug("ajax redirect");
sendRedirect(ctx.getResponse(),redirectUrl);
}
//如果是瀏覽器地址欄請(qǐng)求
else {
log.debug("normal redirect ");
ctx.getResponse().sendRedirect(redirectUrl);
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
/**
*功能描述 
* @author lgj
* @Description Ajax請(qǐng)求時(shí)重定向處理
* @date 2/27/19
* @param: 
* @return: 
*
*/
private static void sendRedirect(HttpServletResponse response, String redirectUrl){
try {
       //這里并不是設(shè)置跳轉(zhuǎn)頁(yè)面,而是將重定向的地址發(fā)給前端,讓前端執(zhí)行重定向
//設(shè)置跳轉(zhuǎn)地址
response.setHeader("redirectUrl", redirectUrl);
//設(shè)置跳轉(zhuǎn)使能
response.setHeader("enableRedirect","true");
response.flushBuffer();
} catch (IOException ex) {
log.error("Could not redirect to: " + redirectUrl, ex);
}
}
}

前端處理

第一種方式:使用Ajax的complete方法

$.ajax({
type: "post",
url: "/auth/token/check",
success: function(data,status){
console.log("/token/check 返回 status : "+status)
},
//請(qǐng)求完成調(diào)用
(XHR, TS){
console.log("complete");
var url = XHR.getResponseHeader("redirectUrl");
console.log("redirectUrl = " + url);
var enable = XHR.getResponseHeader("enaleRedirect");
console.log("enableRedirect = " + enable);
if((enable == "true") && (url != "")){ 
var win = window; 
         while(win != win.top){
      win = win.top;
        }
         win.location.href = url; 
          } 
      }, 
    });
})

但是上面有個(gè)問(wèn)題就是,每個(gè)ajax都需要編寫 comlete 方法,代碼復(fù)用率低。

第二種方法 : 使用全局的complete方法

ajax請(qǐng)求

$("#NON-TOKEN").click(function () {
$.ajax({
type: "post",
url: "/auth/token/check",
success: function(data,status){
console.log("/token/check 返回 status : "+status)
},
});

全局處理

注意這參數(shù)(event, xhr, settings)不能少,否則會(huì)報(bào)錯(cuò)。

//每一個(gè)Ajax 請(qǐng)求完成之后都會(huì)執(zhí)行。
$(document).ajaxComplete(function (event, xhr, settings) {
console.log("ajaxComplete ")
redirectHandle(xhr);
})
function redirectHandle(xhr) {
  //獲取后臺(tái)返回的參數(shù)
var url = xhr.getResponseHeader("redirectUrl");
console.log("redirectUrl = " + url);
var enable = xhr.getResponseHeader("enableRedirect");
if((enable == "true") && (url != "")){
var win = window;
while(win != win.top){
win = win.top;
}
win.location.href = url;
}
}

也可以將上述前端代碼放入一個(gè)js文件中,在需要進(jìn)行重定向的時(shí)候引入即可,便可以實(shí)現(xiàn)更高的代碼復(fù)用率。

redirect.js

function redirectHandle(xhr) {
var url = xhr.getResponseHeader("redirectUrl");
console.log("redirectUrl = " + url);
var enable = xhr.getResponseHeader("enableRedirect");
if((enable == "true") && (url != "")){
var win = window;
while(win != win.top){
win = win.top;
}
win.location.href = url;
}
}
$(function () {
$(document).ajaxComplete(function (event, xhr, settings) {
console.log("ajaxComplete adffdafadsaf")
redirectHandle(xhr);
})
})

引入js文件,src根據(jù)據(jù)實(shí)際情況設(shè)置。

<script src="/common/redirect.js"></script>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論