【发布时间】:2019-11-03 06:23:02
【问题描述】:
我正在使用nestJs => 6.3.1 框架开发一个Node 基础项目。我已启用所有 cors 仍然面临以下错误
从源“http://localhost:4200”访问“localhost:3000”处的 XMLHttpRequest 已被 CORS 策略阻止:跨源请求仅支持以下协议方案:http、data、chrome、chrome-extension、https。
core.js:7187 ERROR 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);
-
var app = await NestFactory.create(AppModule); 常量选项 = { 起源: '*', 方法:'GET,HEAD,PUT,PATCH,POST,DELETE', preflightContinue:假, 选项成功状态:204, 凭据:真实, allowedHeaders: '内容类型,接受', }; 控制台日志(应用程序); app.enableCors(选项); 等待 app.listen(3000);
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app/app.module'; async function bootstrap() { 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', }; console.log(app); app.enableCors(options); await app.listen(3000); } bootstrap();
预期输出:服务器应该允许处理跨源请求,但它给出了以下问题或错误。
从源“http://localhost:4200”访问“localhost:3000”处的 XMLHttpRequest 已被 CORS 策略阻止:跨源请求仅支持以下协议方案:http、data、chrome、chrome-extension、https。
【问题讨论】:
标签: nestjs