vue axios 配置上服務器后報錯'Access-Control-Allow-Origin' header]


ue項目配置到服務器后,請求能夠成功,返回的數據也能在瀏覽器中看見,但是報錯:

Failed to load http://pre.api.jmxy.mockuai.c...: 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://pre.promotion.jmxy.moc...' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

clipboard.png

請求狀態碼為200. 在瀏覽器中也能看到返回的數據:

我配置的axios為:

 
var instance = axios.create({ baseURL: baseUrl, timeout: 1000 * 12, // 創建axios實例,設定超時時間是12s withCredentials: true, // 允許攜帶cookie headers: { "Content-Type": "application/x-www-form-urlencoded;charset=utf-8" } });

解決方法:

withCredentials: false, // 允許攜帶cookie

在axios里把這一條設置為false就行了,我們的項目是不需要攜帶cookie的,所以之前的項目后端都沒有改過,我剛接手,所以不太清楚,才鬧出這個問題,如果需要攜帶cookie的話,我搜了一圈下來,基本上都和下面幾位的回答的是一樣的,需要后端配置。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM