最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

CORS正在使用凭据阻止请求

IT培训 admin 13浏览 0评论

CORS正在使用凭据阻止请求

我是Node和Angular的新手。发送请求withCredentials: true选项时,Cors出现问题。当我发送这样的请求时,我得到:

响应中'Access-Control-Allow-Origin'标头的值当请求的凭据模式为时,不得为通配符'*''include'.`。

[不使用此选项发送请求时,我没有错误。 Angular在http://localhost:4200上工作,而Nodejs在http://localhost:1234上工作。似乎cors模块无法正常工作。

在NodeJs服务器中,我使用cors模块:

const cors = require('cors');

const corsOptions = {
  origin: 'http://localhost:4200',
  optionsSuccessStatus: 200,
  credentials: true
};

app.use(cors(corsOptions));

服务器上的请求标头:

{ host: 'localhost:1234',
  connection: 'keep-alive',
  'content-length': '44',
  accept: 'application/json, text/plain, */*',
  origin: 'http://localhost:4200',
  'user-agent':
   'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
  'content-type': 'application/json',
  'sec-fetch-site': 'same-site',
  'sec-fetch-mode': 'cors',
  referer: 'http://localhost:4200/login',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7' }
回答如下:中的'Access-Control-Allow-Origin'标头的值

您可以通过设置标题来尝试:

app.use(function (req, res, next) {

    // UI port you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
    ...
}

UI:

this.http.get(url, { withCredentials: true })

当我们将凭据传递给后端服务时,必须指定url端口。

CORS正在使用凭据阻止请求

我是Node和Angular的新手。发送请求withCredentials: true选项时,Cors出现问题。当我发送这样的请求时,我得到:

响应中'Access-Control-Allow-Origin'标头的值当请求的凭据模式为时,不得为通配符'*''include'.`。

[不使用此选项发送请求时,我没有错误。 Angular在http://localhost:4200上工作,而Nodejs在http://localhost:1234上工作。似乎cors模块无法正常工作。

在NodeJs服务器中,我使用cors模块:

const cors = require('cors');

const corsOptions = {
  origin: 'http://localhost:4200',
  optionsSuccessStatus: 200,
  credentials: true
};

app.use(cors(corsOptions));

服务器上的请求标头:

{ host: 'localhost:1234',
  connection: 'keep-alive',
  'content-length': '44',
  accept: 'application/json, text/plain, */*',
  origin: 'http://localhost:4200',
  'user-agent':
   'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
  'content-type': 'application/json',
  'sec-fetch-site': 'same-site',
  'sec-fetch-mode': 'cors',
  referer: 'http://localhost:4200/login',
  'accept-encoding': 'gzip, deflate, br',
  'accept-language': 'pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7' }
回答如下:中的'Access-Control-Allow-Origin'标头的值

您可以通过设置标题来尝试:

app.use(function (req, res, next) {

    // UI port you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
    ...
}

UI:

this.http.get(url, { withCredentials: true })

当我们将凭据传递给后端服务时,必须指定url端口。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论