In actual development, we often need to obtain resources from other domains. This article mainly introduces the summary of several Javascript cross-origin methods. If you are interested, you can take a look. In the client programming language, for example, javascript, the same-origin policy specifies that scripts in different domains are isolated. scripts in one domain cannot access and operate most of the attributes and methods of another domain. Only when two domains have the same protocol, the same host, and the same port, we think they are the same domain. However, in actual development, we often need to obtain resources from other domains. At this time, various cross-origin resource methods are quite different. Today we will mainly summarize several cross-origin methods commonly used in our work, for query.
1. window. name
The name attribute of the window object is a special attribute. When loading a new page in the frame, the attribute value of name remains unchanged. In this case, we can use iframe to load page B of other domains in page A, while in page B, we use JavaScript to assign the data to window. name, iframe after loading, the name attribute value can be obtained to access the information sent by the Web service. However, the name attribute is only accessible to frames with the same domain name. This means that in order to access the name attribute, after the remote Web Service Page is loaded, the frame must be navigated back to the original domain. That is, page A modifies the iframe address, converts it to an address in the same domain, and then reads the value of window. name. Destroy the frame once the name attribute is obtained. This method is suitable for one-way data requests, and the protocol is simple and secure.
The code for page B (www.jesse.com/data.html) is as follows: