Angular 11 CORS 问题代理端口正在递增
Angular 11 CORS issue proxy port being incremented
我正在目睹一些我真的不喜欢的事情,我认为这是我的问题的根源:
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"
}
}
我在“angular.json”中加载,感谢“proxyConfig”:设置 (angular docs) :
"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
CORS 错误令人困惑。但我认为这可能是您的 API 不接受来自前端的呼叫。我看到您在后端使用 C#。仔细检查它是否接受来自本地主机的调用(或 *,如果您不担心安全问题)。我知道 ASP.NET 有时会成为 CORS 的痛点。
我正在目睹一些我真的不喜欢的事情,我认为这是我的问题的根源:
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"
}
}
我在“angular.json”中加载,感谢“proxyConfig”:设置 (angular docs) :
"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
CORS 错误令人困惑。但我认为这可能是您的 API 不接受来自前端的呼叫。我看到您在后端使用 C#。仔细检查它是否接受来自本地主机的调用(或 *,如果您不担心安全问题)。我知道 ASP.NET 有时会成为 CORS 的痛点。