欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Ajax請求時無法重定向怎么辦

這篇文章給大家分享的是有關(guān)Ajax請求時無法重定向怎么辦的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

資源ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

前言

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

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

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

后臺:

/**
*功能描述
* @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請求
if("XMLHttpRequest".equals(ctx.getRequest().getHeader("X-Requested-With"))){
log.debug("ajax redirect");
sendRedirect(ctx.getResponse(),redirectUrl);
}
//如果是瀏覽器地址欄請求
else {
log.debug("normal redirect ");
ctx.getResponse().sendRedirect(redirectUrl);
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
/**
*功能描述 
* @author lgj
* @Description Ajax請求時重定向處理
* @date 2/27/19
* @param: 
* @return: 
*
*/
private static void sendRedirect(HttpServletResponse response, String redirectUrl){
try {
       //這里并不是設(shè)置跳轉(zhuǎn)頁面,而是將重定向的地址發(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)
},
//請求完成調(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; 
          } 
      }, 
    });
})

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

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

ajax請求

$("#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)不能少,否則會報錯。

//每一個Ajax 請求完成之后都會執(zhí)行。
$(document).ajaxComplete(function (event, xhr, settings) {
console.log("ajaxComplete ")
redirectHandle(xhr);
})
function redirectHandle(xhr) {
  //獲取后臺返回的參數(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;
}
}

也可以將上述前端代碼放入一個js文件中,在需要進(jìn)行重定向的時候引入即可,便可以實現(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è)置。

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

感謝各位的閱讀!關(guān)于“Ajax請求時無法重定向怎么辦”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

文章名稱:Ajax請求時無法重定向怎么辦
網(wǎng)頁地址:http://www.chinadenli.net/article20/ihedco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站標(biāo)簽優(yōu)化營銷型網(wǎng)站建設(shè)網(wǎng)站營銷商城網(wǎng)站外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計