【问题标题】:Cors still blocking requests after setting them on the server在服务器上设置请求后,Cors 仍然阻止请求
【发布时间】:2020-01-12 14:08:38
【问题描述】:

即使我在服务器上启用了 CORS,Chrome CORS 仍会阻止 API。我阅读了官方文档以启用 CORS。我的后端是用 Nest.js 编写的,前端是用 Ionic/Angular 编写的。

除了官方文档之外,我还尝试了各种其他用户建议的解决方案,例如设置选项,但它们都不起作用。

使用邮递员时,API 调用工作得非常好。

这是服务器代码:

const app = await NestFactory.create(AppModule);
  const options = {
    origin: '*',
    headers: '*',
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    preflightContinue: true,
    optionsSuccessStatus: 204,
    credentials: true,
  };
  app.enableCors(options);
  await app.listen(3000);

这是 http 调用:

  this.http.post(`${this.config.serverAddress}/api/auth/login`,{'username': 'test', 'password': 'password'})
        .subscribe(d=>{
            console.log(d);
        })

最后,这是 chrome 错误消息:

zone-evergreen.js:2952 Access to XMLHttpRequest at 'localhost:3000/api/auth/login' from origin 'http://localhost:8100' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

【问题讨论】:

    标签: javascript node.js angular typescript nestjs


    【解决方案1】:

    您的网址中缺少协议。如错误所述,仅支持以下协议:

    http、数据、chrome、chrome-extension、https。

    所以试试http://localhost:3000/api/auth/login的请求

    【讨论】:

      猜你喜欢
      • 2015-03-09
      • 1970-01-01
      • 2021-02-04
      • 2018-10-18
      • 2020-08-16
      • 2022-11-01
      • 1970-01-01
      • 2018-06-28
      • 2020-09-06
      相关资源
      最近更新 更多