前提:跨域狀況(同域下無效)vue
vue項目中使用proxy代理服務,會轉爲同域,設置withCredentials=true,將不會形成影響,但發佈到服務器出現跨域,將沒法訪問服務端接口,所以發佈服務器不要採用代理。後端
一、Access-Control-Allow-Origin不能爲*,不然將報錯,以下:跨域
Failed to load http://localhost:3000/login: 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 'https://192.168.125.15:9527' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
服務器
二、配合Access-Control-Allow-Credentials一塊兒使用,不然將報錯,以下:解決辦法:須要在後端增長Access-Control-Allow-Origin的須要跨域地址cookie
Failed to load http://localhost:3000/login: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'https://192.168.125.15:9527' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
解決辦法:增長Access-Control-Allow-Credentialsapp
響應首部Access-Control-Allow-Headers用於預檢請求中列出將會在正式請求Access-Control-Request-Headers中容許的信息url
注意如下這些特定的首部是一直容許的:Accept, Accept-Language, Content-Language, Content-Type (但只在其值屬於 MIME 類型 application/x-www-form-urlencoded, multipart/form-data 或 text/plain中的一種時)。這些被稱做simple headers,你無需特地聲明它們。3d
Failed to load http://localhost:3000/login: Request header field token is not allowed by Access-Control-Allow-Headers in preflight response.
代理