原文:https://www.jb51.net/article/137278.htm前端
前端 vue 框架,跨域問題後臺加這段代碼
header("Access-Control-Allow-Origin: *");
加了以後報這個錯:vue
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 value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
文章連接:CORS: credentials mode is ‘include'json
xhrFields: { withCredentials: false },
把 withCredentials: true 改爲 withCredentials: false,若是你沒加上面那段代碼固然也不會報這個錯。雖然是解決方法很簡單,但經此發現許多知識沒掌握不得不梳理下。跨域
跨域請求想要帶上 cookies 必須在請求頭裏面加上:瀏覽器
crossDomain: true, xhrFields: { withCredentials: true }
又變成文章開頭的問題了,解決辦法:服務器
後臺代碼:cookie
Access-Control-Allow-Origin: 'http://www.zrt.local:8080' Access-Control-Allow-Credentials: true
前端代碼:app
crossDomain: true, xhrFields: { withCredentials: true }
跟以前同樣就好了。cors
.NET CORE框架
services.AddCors( options => options.AddPolicy( _defaultCorsPolicyName, builder => builder .AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials() ) );
PHP後臺代碼:
header("Access-Control-Allow-Origin: *");
前端代碼:
xhrFields: { withCredentials: false },
.NET CORE
services.AddCors( options => options.AddPolicy( _defaultCorsPolicyName, builder => builder .WithOrigins( // App:CorsOrigins 在 appsettings.json 中,可配多個地址 _appConfiguration["App:CorsOrigins"] .Split(",", StringSplitOptions.RemoveEmptyEntries) .Select(o => o.RemovePostFix("/")) .ToArray() ) .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials() ) );
PHP後臺代碼:
Access-Control-Allow-Origin: 'http://www.zrt.local:8080' Access-Control-Allow-Credentials: true
前端代碼:
crossDomain: true, xhrFields: { withCredentials: true }