文章目錄
- 前言
- 一、修改之前
- 1.登錄
- 2.登錄之后讀取
- 二、解決方案
- 三、修改之后
- 1.登錄
- 2.登錄之后讀取
前言
這次專案登錄的時候利用axios傳輸的時候發現后臺接收的到sessionID但是回傳前臺的時候cookie卻沒有sessionID,
網上大多數都是axios.defaults.withCredentials = true,但是沒用,解決了一下午,終于找到了答案
一、修改之前
1.登錄
代碼如下(示例):
axios({
widthCredentials: true,
method: 'post',
url: "http://location:8080/api/guojia/all/login",
data: _this.form
}).then(res => {
console.log(res)
if (res.data != null && res.data != "") {
localStorage.setItem("position", res.data.position.pname)
sessionStorage.setItem("tabs", "");
location.href = "../../index.html"
} else {
this.$message.error('賬號或者密碼錯誤');
}
})
2.登錄之后讀取
代碼如下(示例):
axios({
method: 'get',
url: "http://127.0.0.1:8080/api/guojia/all/getUser",
widthCredentials: true
}).then(res => {
_this.User = res.data
$(".allpage").hide();
showsItem = _this.User.position.operatingList;
pageInit()
$("#hhhh").load("index2.html")
})
照網上大多數的方法,我已經加上了axios.defaults.withCredentials = true,但是還是沒有解決
二、解決方案
不知道大家注意到沒有我登錄的URL和讀取的URL有什么不同,在登錄的時候我寫的是http://location:8080,看群友說,有時候location回傳時前端無法保存到cookie,這是錯誤一,錯誤二是讀取的時候我寫的是http://127.0.0.1:8080,這就導致了兩次訪問的地址不一樣,當然取不到SessionID
三、修改之后
1.登錄
代碼如下(示例):
axios({
widthCredentials: true,
method: 'post',
url: "http://127.0.0.1:8080/api/guojia/all/login",
data: _this.form
}).then(res => {
console.log(res)
if (res.data != null && res.data != "") {
localStorage.setItem("position", res.data.position.pname)
sessionStorage.setItem("tabs", "");
location.href = "../../index.html"
} else {
this.$message.error('賬號或者密碼錯誤');
}
})
2.登錄之后讀取
代碼如下(示例):
axios({
method: 'get',
url: "http://127.0.0.1:8080/api/guojia/all/getUser",
widthCredentials: true
}).then(res => {
_this.User = res.data
$(".allpage").hide();
showsItem = _this.User.position.operatingList;
pageInit()
$("#hhhh").load("index2.html")
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/275827.html
標籤:其他
