【问题标题】:Angular 13 : has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resourceAngular 13:已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头
【发布时间】:2022-07-19 04:06:25
【问题描述】:

谁能帮帮我,我的 CORS 政策有问题,我无法访问网站的后端。

这是我在后端(node.js)中使用的代码:

app.use(cors({
  Access_Control_Allow_Origin: "*",
  origin:"*",
  methode:['GET','POST','PATCH','DELETE','PUT'],
  allowedHeaders:'Content-Type, Authorization, Origin, X-Requested-With, Accept'

})); 

这是前端(我使用 Angular 13)导入 API 的代码: 环境.ts

export const environment = {
  production: false,
  serverURL: 'http://localhost:3000/api/'
};

我也试过这段代码,但没有用:

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

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

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
});

【问题讨论】:

  • 我会再次检查docsAccess_Control_Allow_Origin 是......但不是 cors 包的一部分,methode 拼写错误,您有 http://localhost:8888 作为允许来源,但 Angular 在 4200 上运行,等等。从文档开始并慢慢添加。跨度>
  • 愿意打赌是因为methode
  • 我从其他成员的响应中复制了此代码,并且我已将代码的来源更改为 4200,但它不起作用
  • 试试第二个答案。这解决了我的问题。参考这个stackoverflow.com/questions/57009371/…

标签: angular visual-studio-code opera swagger-node-express


【解决方案1】:

确保您有正确的导入:

var cors = require('cors')

var app = express()

或者使用:

app.use(cors()) // this uses default values

或者把你的代码改成这样:

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

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', '*');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);

    // Pass to next layer of middleware
    next();
});

在生产环境中小心使用“*”作为 Access-Control-Allow-Origin。仅将其更改回允许连接到您的 API 的客户端。

如果没有帮助,请尝试设置代理请求以在 Angular 中启用 CORS:在应用程序的 src 文件夹中,创建一个名为 proxy.conf.json 的新文件。这是一个 JSON 文件,将包含代理服务器的配置。在这里,我们将告诉 Angular 应用程序充当另一个服务器,接受 API 调用并将它们转移到 http://localhost:3000。

在 proxy.conf.json 文件中,添加以下代码:

{
    "/api": {
        "target": "http://localhost:3000",
        "secure": false
    }
}

接下来,通知 Angular 这个代理配置。我们可以通过在 angular.json 文件中添加 proxyConfig 选项来做到这一点:

...
"architect": {
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "your-application-name:build",
"proxyConfig": "src/proxy.conf.json"
}

【讨论】:

  • thnx 但它没有用..
  • 你遇到了什么错误?
  • No 'Access-Control-Allow-Origin' ,API 链接有效,我用“Postman”测试它,但它在 Angular 上不起作用,
  • 有时间可以和我核对一下代码吗,很重要
  • 我修改了答案。请检查这是否对您有帮助。
猜你喜欢
  • 2019-12-05
  • 1970-01-01
  • 2020-01-24
  • 2021-12-27
  • 2020-11-23
  • 1970-01-01
  • 2020-04-16
  • 2021-04-28
  • 2022-06-21
相关资源
最近更新 更多