What is a CORS error and how to fix it?

When accessing the server through http://localhost, what is the problem of CROS error and how to solve it?

Reasons for ajax cross-domain problems: (Cross-domain problems may occur only when three reasons are met at the same time)
(1) Browser restrictions

  When the ajax cross-domain problem occurs, the backend is executed normally, as can be seen from the log printed in the background, and the background will return data normally. The browser has restrictions for security. To put it bluntly, the browser is nosy.

(2) Cross domain:

  When the protocol, domain name, and port are inconsistent, the browser will consider it a cross-domain problem.

(3) XHR (XMLHttpRequest) request, that is, ajax request

  If it is not an ajax request, there is no cross-domain problem (we should be able to understand this, direct browser access and a tag jumps will not cause cross-domain problems).
————————————————
Copyright statement: This article is the original article of the CSDN blogger "Working Hard Network Security Officer", following the CC 4.0 BY-SA copyright agreement, please attach the original source for reprinting link and this statement.
Original link: https://blog.csdn.net/weixin_49875821/article/details/120696232

CROS has a cross-domain error report because by default, cross-domain access is not allowed, especially in the front-end and back-end separation mode, to achieve front-end and back-end interaction, it needs to be configured on the server side to allow cross-domain access!

Create a class under the root package of the project config.WebMvcConfiguration, add annotations to the class @Configuration, and implement WebMvcConfiguruerthe interface, rewrite the method in the interface addCorsMappings()to configure and allow cross-domain access:

 At this time, as long as we finish this configuration class, we can successfully solve the error reporting of browser CROS, let’s try it quickly

Guess you like

Origin blog.csdn.net/weixin_71583566/article/details/126653669