javascript xmlhttp error on signalr in asp.net core

柔情痞子 提交于 2021-02-10 22:22:20

问题


In my application 2 projects and mvc client run at port(5002) and web api project run at port (5001). I have implemented signalr in mvc client. Now showing error log in console as below:

and i have also added configuration to my api project for core policy like:

services.AddCors(options =>
{
    options.AddPolicy("signalr",
        builder =>
        {
            builder.WithOrigins("https://localhost:5002")
                   .AllowAnyHeader()
                   .AllowAnyMethod();
        });
});

And now also showing same error. Please suggest.


回答1:


You need to configure your CORS like this:

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.



来源:https://stackoverflow.com/questions/62958384/javascript-xmlhttp-error-on-signalr-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!