当请求的凭证模式为 'include' 时,响应中 'Access-Control-Allow-Origin' header 的值不得为通配符“*”。-
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'.-
我正在尝试将我的 angular 应用程序与带有 graphql api 的 nodejs 连接,并且我正在使用 cookies/sessions 进行身份验证。
api 与 GraphQL PLayground 完美配合。
这是我的 angular 连接,用于连接 graphql api。
const uri = "http://localhost:4000/graphql/"; // <-- add the URL of the GraphQL server here
export function createApollo(httpLink: HttpLink): ApolloClientOptions<any> {
return {
link: httpLink.create({ uri, withCredentials: true }),
cache: new InMemoryCache(),
};
}
如果我设置 withCredentials:false
就建立了连接,但我不能再使用 cookie 进行身份验证了。
这是我在 cors 中间件上的代码。
const corsOptions = {
credentials: true,
origin: "http://localhost:4200",
};
app.use(cors(corsOptions));
我已经尝试了以下代码片段来完成这项工作,但 none 成功了。
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:4200");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept",
);
res.header("Access-Control-Allow-Credentials", "true");
next();
});
const corsOptions = {
credentials: true,
origin: true,
};
app.use(cors(corsOptions));
这正是显示的错误。
我已经尝试了下面问题 link 的所有解决方案。
相关问题:
- CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
- Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?
...和类似的帖子,但 none 已经解决了这个问题。
您必须在创建服务器时设置 apolloServer.applyMiddleware({ app, cors: false });
,否则应用程序中似乎有两个 cors 拦截器,其中任何一个都会抛出任何试图连接的客户端,
使用 app.use(cors(....))
将 cors 设置添加到顶部,或者在添加 apolloServer 作为中间件时添加它。
我正在尝试将我的 angular 应用程序与带有 graphql api 的 nodejs 连接,并且我正在使用 cookies/sessions 进行身份验证。
api 与 GraphQL PLayground 完美配合。 这是我的 angular 连接,用于连接 graphql api。
const uri = "http://localhost:4000/graphql/"; // <-- add the URL of the GraphQL server here
export function createApollo(httpLink: HttpLink): ApolloClientOptions<any> {
return {
link: httpLink.create({ uri, withCredentials: true }),
cache: new InMemoryCache(),
};
}
如果我设置 withCredentials:false
就建立了连接,但我不能再使用 cookie 进行身份验证了。
这是我在 cors 中间件上的代码。
const corsOptions = {
credentials: true,
origin: "http://localhost:4200",
};
app.use(cors(corsOptions));
我已经尝试了以下代码片段来完成这项工作,但 none 成功了。
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:4200");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept",
);
res.header("Access-Control-Allow-Credentials", "true");
next();
});
const corsOptions = {
credentials: true,
origin: true,
};
app.use(cors(corsOptions));
这正是显示的错误。
我已经尝试了下面问题 link 的所有解决方案。
相关问题:
- CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
- Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?
...和类似的帖子,但 none 已经解决了这个问题。
您必须在创建服务器时设置 apolloServer.applyMiddleware({ app, cors: false });
,否则应用程序中似乎有两个 cors 拦截器,其中任何一个都会抛出任何试图连接的客户端,
使用 app.use(cors(....))
将 cors 设置添加到顶部,或者在添加 apolloServer 作为中间件时添加它。