javascript asp.net 核心中信号器的 xmlhttp 错误
javascript xmlhttp error on signalr in asp.net core
在我的应用程序中,2 个项目和 mvc 客户端 运行 在端口 (5002) 和 Web api 项目 运行 在端口 (5001)。我在 mvc 客户端中实现了 signalr。现在在控制台中显示错误日志如下:
而且我还在我的 api 项目中添加了核心策略配置,例如:
services.AddCors(options =>
{
options.AddPolicy("signalr",
builder =>
{
builder.WithOrigins("https://localhost:5002")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
现在也显示同样的错误。请提出建议。
您需要像这样配置您的 CORS:
services.AddCors(options =>
{
options.AddPolicy("signalr", builder => builder.WithOrigins("https://localhost:5002")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((host) => true));
});
The lambda function that you pass to the .SetIsOriginAllowed()
method returns true if an origin is allowed, so always returning true allows any origin to send requests to the api. The allow origin access control http header returned when using this method contains the origin that sent the request, not a wildcard, e.g. Access-Control-Allow-Origin: http://localhost:4200.
在我的应用程序中,2 个项目和 mvc 客户端 运行 在端口 (5002) 和 Web api 项目 运行 在端口 (5001)。我在 mvc 客户端中实现了 signalr。现在在控制台中显示错误日志如下:
而且我还在我的 api 项目中添加了核心策略配置,例如:
services.AddCors(options =>
{
options.AddPolicy("signalr",
builder =>
{
builder.WithOrigins("https://localhost:5002")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
现在也显示同样的错误。请提出建议。
您需要像这样配置您的 CORS:
services.AddCors(options =>
{
options.AddPolicy("signalr", builder => builder.WithOrigins("https://localhost:5002")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((host) => true));
});
The lambda function that you pass to the
.SetIsOriginAllowed()
method returns true if an origin is allowed, so always returning true allows any origin to send requests to the api. The allow origin access control http header returned when using this method contains the origin that sent the request, not a wildcard, e.g. Access-Control-Allow-Origin: http://localhost:4200.