First, the cross-domain is due to the browser’s “same-origin policy,” which has nothing to do with the server. Sites between different sources because of the “same origin policy” :
- Unable to access data stored in the browser, for example
localStorage
.IndexedDB
.Cookie
- DOM is not available
- The Ajax request cannot be sent
Communication between different source sites can be done in the following ways:
- By setting the same
document.domain
Can be Sharedcookie
, can also be used to get each other in the parent-child windowDom
- through
window.onhashchange
Shared hash values for parent-child Windows only (inline iframe) - through
postmessage
As for Ajax requests, the browser will first check whether the request path is homology with the current page, and the homology will be sent directly, and different sources will be held first, and an Origin attribute will be added to the header of the request to send a “Pre-Check Request”. After receiving the request, the server will return the result according to its own configuration
Access-Control-Allow-Origin
: A list of domain names allowed across domainsAccess-Control-Allow-Methods
: Request mode that allows crossingAccess-Control-Allow-Headers
: Allows cross-domainHeader
The list ofAccess-Control-Expose-Headers
: Allow exposure toJavaScript
The code ofHeader
The list ofAccess-Control-Max-Age
: Maximum browser cache time, ins
According to the returned data, the browser will send a formal request if it meets the conditions, and refuse to send the request if it does not.
That’s what CORS is, but CORS doesn’t send a precheck request when it’s a simple request