Detailed explanation of javascript iframe cross-domain

高洛峰
Release: 2016-12-08 16:18:39
Original
1141 people have browsed it

1. What causes the problem that ajax cannot cross domain?

ajax itself actually interacts with data through the XMLHttpRequest object. However, for security reasons, the browser does not allow js code to perform cross-domain operations, so a warning will be issued.

2. Is there any perfect solution?

No. There are many solutions, but you can only choose based on your actual situation.

The specific situations are:

1. Mutual access between this domain and sub-domains: www.aa.com and book.aa.com
2. Mutual access between this domain and other domains: www.aa.com and www. bb.com uses iframe
3. Mutual access between this domain and other domains: www.aa.com and www.bb.com Use XMLHttpRequest to access the proxy
4. Mutual access between this domain and other domains: www.aa.com and www.bb.com Use JS to create dynamic scripts

Solution:

1. If you want to achieve data interaction, then www.aa.com and book.aa.com must be developed by you. You can add book.aa.com to a page of www.aa.com using an iframe, and add document.domain = "aa.com" to both www.aa.com and the iframe, so that the domains can be unified. , cross-domain access can be achieved. Just like embedding an iframe in the same domain, just call the JS inside it directly. (I have not tried this method, but it is theoretically feasible)

2. When the two domains are different, if you want to call each other, then both domains also need to be developed by you. Using iframe, data can be transferred to each other. The solution is to use the hash property of the window.location object. The hash attribute is #dshakjdhsjka in http://domian/web/a.htm#dshakjdhsjka. If you use JS to change the hash value, the web page will not be refreshed. You can access the hash value through JS to achieve communication. However, except for IE, most other browsers will record the history as long as the hash is changed, and you need to process it when going forward and backward, which is very troublesome. However, it can still be used for simple processing. I will download the specific code below. The general process is that page a and page b are in different domains, b is added to a through iframe, a modifies the hash value of iframe through JS, and a monitor is made in b (because JS can only modify the hash, whether the data is changed can only be changed by b will judge by himself), detect that the hash value of b has been modified, get the modified value, return the value required by a after processing, and then modify the hash value of a (you should pay attention to this, if a itself is the kind of query page) For example, http://domian/web/a.aspx?id=3, it is impossible to obtain data directly in parent.window.location in b. It also reports an error of no permission. A needs to pass this over, so it is more troublesome. ), also need to be monitored in a. If the hash changes, the returned data will be obtained, and then processed accordingly.

3. This situation is the most frequently encountered and the most used. You can only modify one of www.aa.com and www.bb.com, which means the other one belongs to someone else. People tell you what the connection parameters look like when you want to get data, and what format the final returned data is. of. What you need to do is to create a new web page under your domain, let the server get the data from other people's websites, and then return it to you. a under domain1 requests data from GetData.aspx under the same domain, GetData.aspx sends a request to ResponseData.aspx under domain2, ResponseData.aspx returns the data to GetData.aspx, and GetData.aspx returns it to a. This is done. A data request. GetData.aspx acts as a proxy. You can see my code for details.

4. The difference between this one and the previous one is that the request is made using the

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!