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.
請求狀態碼為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的話,我搜了一圈下來,基本上都和下面幾位的回答的是一樣的,需要后端配置。