我像这样使用Angular连接到服务器:
this._hubConnection = new signalR.HubConnectionBuilder()
.withUrl(location.protocol + '//localhost:5000/notify')
.configureLogging(signalR.LogLevel.Information)
.build();
代码或主机没有问题,请求是有效的。
但是库会发起额外的调用:
http://localhost:5000/notify/negotiate
然后我得到了这个错误:
HTTP ERROR 405
客户端显示的完整错误信息如下:
除了上述请求外,我在该网站中没有跨域问题。Failed to load http://localhost:5000/notify/negotiate: Response to
preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
我已经在startup.cs中进行了配置。
app.UseCors(builder =>
builder.WithOrigins("http://localhost:4200").AllowAnyMethod
().AllowAnyHeader().AllowAnyOrigin());
这段代码适用于整个网站。
为什么其他请求都能正常工作,突然出现跨域错误?
UseCors
调用中,您有WithOrigins(...)
和AllowAnyOrigin()
。尝试移除AllowAnyOrigin()
调用,看看是否可以解决问题? - Simply Ged