我见证了一些我真的不喜欢的事情,我认为这是我问题的根源:
Access to XMLHttpRequest at 'https://localhost:5001/calls/api/Auth/register' (redirected from 'http://localhost:4200/calls/api/Auth/register') from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Http failure response for /calls/api/Auth/register: 0 Unknown Error
POST https://localhost:5001/calls/api/Auth/register net::ERR_FAILED
这里让我担心的是5001。这是我的"proxy.config.json“:
{
"/calls/*": {
"target": "http://localhost:5000",
"secure": false,
"logLevel": "debug"
}
}
多亏了“proxyConfig”设置,我将其加到"angular.json“中;angular文档):
"proxyConfig": "src/proxy.conf.json"
我可以说这是起作用的,因为当我为应用程序提供服务时,我会得到:
Generating browser application bundles...[HPM] Proxy created: /calls -> http://localhost:5000
我这样调用我的api:
register(email: string, password: string) {
return this.http.post('/calls/api/Auth/register', {
params: {
email: "vb@gmail.com",
password: "ButtsMacgee311",
userName: "newUser"
},
observe: 'response'
}).subscribe(
data => console.log(data),
error => console.error(error.message)
)
}
所以很明显,在我的整个项目中没有提到"5001“,angular的哪个部分是递增到"5001”的,我如何阻止它这样做?
如果这不是真正的问题,那么我做错了什么?
对于上下文,这是一个在我的家庭计算机上运行的个人项目,所以我不必与任何公司代理打交道,而且我可以访问后端源代码,并且我在本地运行自己的版本(从控制台运行),它在C#中,我可以在以下位置访问swagger和测试API:
http://localhost:5000/api-docs/index.html
发布于 2021-02-22 21:04:18
CORS错误令人困惑。但我认为这可能是你的API不接受来自前端的调用。我看到您正在使用C#作为后端。仔细检查它是否接受来自本地主机的调用(或,如果您不担心安全性的话)。我知道使用CORS时ASP.NET有时会让人头疼。
https://stackoverflow.com/questions/66314917
复制相似问题