登錄之后的請求會帶登錄用戶資訊,需要把登錄時的cookie設定到之后的請求頭里面,而跨域請求要想帶上cookie,必須要在vue的main.js里加上axios.defaults.withCredentials = true,withCredentials 屬性是一個Boolean型別,它指示了是否該使用類似cookies,authorization,headers(頭部授權)或者TLS客戶端證書這一類資格證書來創建一個跨站點訪問控制(cross-site Access-Control)請求,
axios.defaults.withCredentials = true;//設定cross跨域 并設定訪問權限 允許跨域攜帶cookie資訊
但是前端這樣設定之后發現報錯了!
Access to XMLHttpRequest at '...' from origin '...' has been blocked by CORS policy: 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'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
原因是:前端設定withCredentials的情況下,后端要設定Access-Control-Allow-Origin為你的源地址,例如http://localhost:8080,不能是*,而且還要設定header(‘Access-Control-Allow-Credentials: true’);
另外,Access-Control-Allow-Origin設定為*,時cookie不會出現在http的請求頭里,所以報錯里說Access-Control-Allow-Origin不能是*,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/169583.html
標籤:其他
上一篇:Chartles功能介紹
