即使通过我的CORS在服务器上启用,Chrome仍然阻塞API。我阅读了官方文件,以使CORS。我的后端是用Nest.js写的,前端用离子/角写的。
除了官方文档之外,我还尝试过其他各种用户建议的解决方案,比如设置选项,但它们都不起作用。
API调用在使用postman时非常好。
以下是服务器代码:
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);
})最后,这里是铬错误消息:
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.发布于 2019-09-10 19:42:07
您在URL中丢失了协议。由于错误状态,只支持这些协议:
http,数据,铬,铬扩展,https。
因此,使用http://localhost:3000/api/auth/login尝试请求
https://stackoverflow.com/questions/57877198
复制相似问题