【发布时间】:2019-03-01 23:40:25
【问题描述】:
我用这样的角度连接到服务器:
this._hubConnection = new signalR.HubConnectionBuilder()
.withUrl(location.protocol + '//localhost:5000/notify')
.configureLogging(signalR.LogLevel.Information)
.build();
代码或主机没有问题。该请求有效。
但随后库会额外调用:
http://localhost:5000/notify/negotiate
然后我得到这个错误:
HTTP 错误 405
客户端中的完整错误是:
Failed to load http://localhost:5000/notify/negotiate: Response to预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://localhost:4200” 访问。
除了上述请求之外,我在网站上没有遇到 cors 问题。
我在 startup.cs 中配置了这个。
app.UseCors(builder =>
builder.WithOrigins("http://localhost:4200").AllowAnyMethod
().AllowAnyHeader().AllowAnyOrigin());
该代码适用于所有网站。
如果其余请求都有效,为什么会突然出现 cors 错误?
【问题讨论】:
-
您在
UseCors通话中有WithOrigins(...)和AllowAnyOrigin()。删除AllowAnyOrigin()调用,看看是否有效?