ASP.NET Core中Signalr上的javascript xmlhttp错误 [英] javascript xmlhttp error on signalr in asp.net core

查看:80
本文介绍了ASP.NET Core中Signalr上的javascript xmlhttp错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,2个项目和mvc客户端在端口(5002)上运行,而Web api项目在端口(5001)上运行.我已经在mvc客户端中实现了signalr.现在在控制台中显示错误日志,如下所示:

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:

而且我还为api策略添加了对我的api项目的配置,例如:

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.

推荐答案

您需要这样配置CORS:

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));
});

您传递给 .SetIsOriginAllowed()方法的lambda函数在允许原点的情况下返回true,因此始终返回true允许任何原点将请求发送到api.使用此方法时返回的allow origin access control http标头包含发送请求的源,而不是通配符,例如Access-Control-Allow-Origin:http://localhost:4200.

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.

这篇关于ASP.NET Core中Signalr上的javascript xmlhttp错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆