8 Scenarios for front-end resolution of Cross-domain problems (latest and most complete)
2013-10-31 19:25 Reading (933) Comments (8) Edit Favorites
1. The homologous strategy is as follows:
|
URL |
Description |
whether to allow communication |
|
Http://www.a.com/a.js
Http://www.a.com/b.js |
Under the same domain name |
Allow |
|
Http://www.a.com/lab/a.js
Http://www.a.com/script/b.js |
Different folders under the same domain name |
Allow |
|
Http://www.a.com:8000/a.js
Http://www.a.com/b.js |
Same domain, different port |
Not allowed |
|
Http://www.a.com/a.js
Https://www.a.com/b.js |
Same domain, different protocol |
Not allowed |
|
Http://www.a.com/a.js
Http://70.32.92.74/b.js |
Domain name and domain name corresponding IP |
Not allowed |
|
Http://www.a.com/a.js
Http://script.a.com/b.js |
Primary domain is the same, subdomain is different |
Not allowed |
|
Http://www.a.com/a.js
Http://a.com/b.js |
Same domain name, different level two domain name (IBID.) |
Not allowed (cookies do not allow access in this case) |
|
Http://www.cnblogs.com/a.js
Http://www.a.com/b.js |
Different domain names |
Not allowed |
Special attention to two points:
First, if the cross domain problem caused by the protocol and the port is "front desk" is powerless,
Second: On cross-domain issues, domains are identified only by the "header of the URL" and do not try to determine whether the same IP address corresponds to two domains or two domains on the same IP.
"Header of a URL" refers to Window.location.protocol +window.location.host, which can also be understood as "Domains, protocols and Ports match".
2. Front-End resolution of Cross-domain problems
1> Document.domain + iframe (this method is only available when the primary domain is the same)
1) in the www.a.com/a.html:
Document.domain = ' a.com ';
var IFR = document.createelement (' iframe ');
IFR.SRC = ' http://www.script.a.com/b.html ';
Ifr.display = none;
Document.body.appendChild (IFR);
Ifr.onload = function () {
var doc = Ifr.contentdocument | | Ifr.contentWindow.document;
Operation Doc here, that's b.html.
Ifr.onload = null;
};
2) in the www.script.a.com/b.html:
Document.domain = ' a.com ';
2> Dynamic Script creation
There's nothing to talk about, because the script label is not limited by the homology policy.
function Loadscript (URL, func) {
var head = Document.head | | Document.getelementbytagname (' head ') [0];
var script = document.createelement (' script ');
script.src = URL;
Script.onload = Script.onreadystatechange = function () {
if (!this.readystate | | this.readystate== ' loaded ' | | | this.readystate== ' complete ') {
Func ();
Script.onload = Script.onreadystatechange = null;
}
};
Head.insertbefore (script, 0);
}
Window.baidu = {
Sug:function (data) {
Console.log (data);
}
}
Loadscript (' Http://suggestion.baidu.com/su?wd=w ', function () {console.log (' Loaded ')});
Where is the content of our request?
We can see what the script introduces in the source of the Chorme debug panel
3> Location.hash + iframe
The principle is to use Location.hash to carry out the value.
Suppose that the file under the domain name a.com cs1.html to communicate with the cs2.html under the cnblogs.com domain name.
1) cs1.html first create an automatic create a hidden iframe,iframe src point to the cs2.html page under the cnblogs.com domain name
2) cs2.html response request will be modified by modifying the cs1.html hash value to pass the data
3 at the same time, add a timer on the cs1.html, at intervals to determine whether the value of Location.hash has changed, once there is a change to get the hash value
Note: Since two pages are not in the same domain IE, chrome is not allowed to modify the value of Parent.location.hash, so to use the A.com domain name under a proxy iframe
The code is as follows:
First the file cs1.html file under a.com:
function Startrequest () {
var IFR = document.createelement (' iframe ');
Ifr.style.display = ' None ';
IFR.SRC = ' Http://www.cnblogs.com/lab/cscript/cs2.html#paramdo ';
Document.body.appendChild (IFR);
}
function Checkhash () {
try {
var data = Location.hash? Location.hash.substring (1): ';
if (Console.log) {
Console.log (' Now ', the data is ' +data ');
}
catch (e) {};
}
SetInterval (Checkhash, 2000);
Cs2.html under the cnblogs.com domain name:
Simulate a simple parameter-handling operation
Switch (Location.hash) {
Case ' #paramdo ':
CallBack ();
Break
Case ' #paramset ':
Do something ...
Break
}
function CallBack () {
try {
Parent.location.hash = ' Somedata ';
catch (e) {
The security mechanism of IE and chrome cannot modify Parent.location.hash,
So to use a proxy iframe in the middle of the cnblogs domain
var ifrproxy = document.createelement (' iframe ');
Ifrproxy.style.display = ' None ';
IFRPROXY.SRC = ' Http://a.com/test/cscript/cs3.html#somedata '; Note that the file is under the "a.com" field
Document.body.appendChild (Ifrproxy);
}
}
A.com under the domain cs3.html
Because Parent.parent and itself belong to the same domain, you can change the value of its Location.hash
Parent.parent.location.hash = self.location.hash.substring (1);
4> window.name + iframe
The beauty of Window.name:name values still exist after different pages (or even different domain names) are loaded, and can support very long name values (2MB).
1) Create a.com/cs1.html
2 Create a.com/proxy.html and add the following code
<script>
function proxy (URL, func) {
var Isfirst = True,
IFR = document.createelement (' iframe '),
Loadfunc = function () {
if (Isfirst) {
ifr.contentWindow.location = ' http://a.com/cs1.html ';
Isfirst = false;
}else{
Func (Ifr.contentWindow.name);
Ifr.contentWindow.close ();
Document.body.removeChild (IFR);
IFR.SRC = ';
IFR = NULL;
}
};
ifr.src = URL;
Ifr.style.display = ' None ';
if (ifr.attachevent) ifr.attachevent (' onload ', loadfunc);
else ifr.onload = Loadfunc;
Document.body.appendChild (IFRAME);
}
</script>
<body>
<script>
Proxy (' http://www.baidu.com/', function (data) {
Console.log (data);
});
</script>
</body>
3 included in the b.com/cs1.html:
<script>
Window.name = ' content to be transmitted ';
</script>
5> PostMessage (API in XMLHttpRequest Level 2 in HTML5)
1) code in a.com/index.html:
<iframe id= "IFR" src= "b.com/index.html" ></iframe>
<script type= "Text/javascript" >
Window.onload = function () {
var IFR = document.getElementById (' IFR ');
var targetorigin = ' http://b.com '; If written as ' http://b.com/c/proxy.html ' effect
If you write ' http://c.com ', you won't execute postmessage.
Ifr.contentWindow.postMessage (' I was there! ', targetorigin);
};
</script>
2) code in b.com/index.html:
<script type= "Text/javascript" >
Window.addeventlistener (' message ', function (event) {
To determine the message source address by Origin property
if (Event.origin = = ' http://a.com ') {
alert (event.data); Pop "I was there!"
alert (Event.source); References to Window objects in A.com, index.html
But because of the homology policy, the window object is not accessible here Event.source
}
}, False);
</script>
6> CORS
The idea behind Cors is to use a custom HTTP header to communicate with the server to determine whether the request or response should be successful or fail.
The implementation of cors in IE is XDR
var xdr = new Xdomainrequest ();
Xdr.onload = function () {
Console.log (Xdr.responsetext);
}
Xdr.open (' Get ', ' http://www.baidu.com ');
......