鍍金池/ 問(wèn)答/Linux  HTML/ vue+fetch+nodejs,session失效問(wèn)題

vue+fetch+nodejs,session失效問(wèn)題

1.將vue項(xiàng)目打包放到node中發(fā)布出來(lái),session獲取沒(méi)有問(wèn)題。
fetch寫法:

let requestConfig = {

  credentials: 'include',
  method: type,
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    "Access-Control-Allow-Credentials": "true"
   },
        mode: "cors",
        cache: "no-cache"
    }

2.前后臺(tái)分離開發(fā)時(shí),session獲取不到,sessionid一直在變

let requestConfig = {

  //credentials: 'include',
  method: type,
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    "Access-Control-Allow-Credentials": "true"
   },
        mode: "cors",
        cache: "no-cache"
    }

credentials: 'include',不注釋的話會(huì)報(bào)錯(cuò):
Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:8001' is therefore not allowed access.

后臺(tái)跨域用的
//解決前臺(tái)請(qǐng)求跨域
app.use(cors());

回答
編輯回答
硬扛

前后端分離開發(fā)的時(shí)候,發(fā)送ajax請(qǐng)求的時(shí)候,不會(huì)把cookie傳過(guò)去,所以每次session會(huì)一直在變

我是使用axios發(fā)送請(qǐng)求的時(shí)候,把 ‘withCredentials’ 設(shè)置為 true

this.$axios.get(${URL}, {withCredentials: true}).then(res => {})

2018年4月13日 14:16