架构师_程序员_码农网

查看: 139|回复: 0

[资料] SignalR 增加远程访问跨域支持

[复制链接]
发表于 2021-4-23 12:39:20 | 显示全部楼层
需求:前端和后端不在一个域名上,所以存在跨域问题,前端如何连接远程 SignalR 呢?

首先,js代码如下:

一开始尝试连接会报错如下:

Access to fetch at 'http://localhost:27939/testHub/negotiate?negotiateVersion=1' from origin 'http://localhost:5009' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
ASP.NET Core 3.1 添加允许跨域

ConfigureServices 代码如下:


Configure 代码如下:

再次尝试连接会报错如下:

QQ截图20210423111002.jpg

Access to fetch at 'http://localhost:27939/testHub/negotiate?negotiateVersion=1' from origin 'http://localhost:5009' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
再次修改 ConfigureServices 代码如下:

QQ截图20210423111344.jpg

问题解决!





上一篇:HTTP 请求头 Expect 详解
下一篇:当代中国经济中期习题课
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2021-5-17 16:35

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表