小编kam*_*iya的帖子

在 Node 框架 NestJS-6.3.1 中面对 CORS

我正在使用 nestJs => 6.3.1 框架开发一个 Node 基础项目。我已启用所有 cors 仍然面临以下错误

CORS 策略已阻止从源“ http://localhost:4200 ”访问“localhost:3000”处的 XMLHttpRequest :跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https。

core.js:7187 错误 HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "localhost:3000", ok: false, ...}

我尝试了以下方法,但仍然面临同样的问题。

1. var app = await NestFactory.create(AppModule,{cors:true}); 等待 app.listen(3000);

  1. var app = await NestFactory.create(AppModule); const options = { origin: '*', methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', preflightContinue: false, optionsSuccessStatus: 204, credentials: true, allowedHeaders: 'Content-Type, Accept', }; 控制台日志(应用程序);app.enableCors(选项); 等待 app.listen(3000);

    import { NestFactory } from '@nestjs/core';
    import { AppModule } from './app/app.module';
    
    async function bootstrap() …
    Run Code Online (Sandbox Code Playgroud)

nestjs

2
推荐指数
1
解决办法
2683
查看次数

标签 统计

nestjs ×1