This article is a mirror article of machine translation, please click here to jump to the original article.

View: 51891|Reply: 2

[Source] SignalR adds cross-domain support for remote access

[Copy link]
Posted on 2021-4-23 12:39:20 | | | |
Requirements: The frontend and backend are not on the same domain name, so there is a cross-domain problem, how can the frontend connect to the remote SignalR?

First, the js code is as follows:

When I try to connect at the beginning, I get the following error:

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 adds allow cross-domain

The ConfigureServices code is as follows:


The Configure code is as follows:

If you try to connect again, you will get the following error:



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'.
Modify the ConfigureServices code again as follows:



Problem solved!





Previous:HTTP request header Expect is explained in detail
Next:Mid-term exercises for contemporary Chinese economy
Posted on 2021-9-22 21:00:27 |
Learn to learn...
Posted on 2022-11-16 14:46:09 |
Learning and learning
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com