Javascript cross-origin Methods

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.