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);
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() {
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();
Run Code Online (Sandbox Code Playgroud)预期输出:服务器应允许处理跨源请求,但出现以下问题或错误。
CORS 策略已阻止从源“ http://localhost:4200 ”访问“localhost:3000”处的 XMLHttpRequest :跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https。
仔细阅读错误信息:
CORS 策略已阻止从源“ http://localhost:4200 ”访问“localhost:3000”处的 XMLHttpRequest :跨源请求仅支持协议方案:http、data、chrome、chrome-extension、https。
您不是通过 HTTP(或 HTTPS)发出请求,因此您的服务器(即 HTTP 服务器)无法响应它,因此您对运行服务器的代码所做的任何事情都不会产生影响。
您需要更改请求 URL 开头的代码localhost:3000并将其更改为http://localhost:3000
| 归档时间: |
|
| 查看次数: |
2683 次 |
| 最近记录: |