我想從 ajax 請求進行重定向。當會話過期時,我的后端重定向到登錄頁面而不是發送資料。但正如之前在 StackOverflow 上討論過的,ajax 不處理重定向。
我沒有一一更新我的所有 ajax 請求來處理狀態401,而是使用以下命令覆寫它們ajaxSetup:
$.ajaxSetup({
beforeSend:(xhr) => {
console.log(xhr);
console.log(xhr.status);
if (xhr.status === 401) {
window.location.replace(_baseUrl);
}
}
});
我正在獲得重定向所需的狀態,但問題是我無法從請求中訪問status變數。XMLHttpRequest
console.log(xhr);:
...
狀態:401
statusCode:?(e)
statusText:“未經授權”
然后:?(t,i,o)
[[原型]]:物件
console.log(xhr.status);:
不明確的
uj5u.com熱心網友回復:
您可以使用.ajaxComplete()event 而不是$.ajaxSetup(). 嘗試這個
$(document).ajaxComplete(function( event, xhr, settings ) {
console.log(xhr);
console.log(xhr.status);
if (xhr.status === 401) {
window.location.replace(_baseUrl);
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/490662.html
上一篇:相關下拉串列在編輯時不顯示值?
