ASP.NET Core中Signalr上的javascript xmlhttp错误

编程入门 行业动态 更新时间:2024-10-26 04:27:04
本文介绍了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("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("localhost:5002") .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials() .SetIsOriginAllowed((host) => true)); });

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

更多推荐

ASP.NET Core中Signalr上的javascript xmlhttp错误

本文发布于:2023-11-12 07:43:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Core   NET   ASP   xmlhttp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!