Home > Web Front-end > JS Tutorial > Common cross-domain solutions for front-end (all)

Common cross-domain solutions for front-end (all)

jacklove
Release: 2018-05-21 16:26:04
Original
1802 people have browsed it

I often see cross-domain related content in my studies. This article will provide cross-domain solutions for the star realm.

Cross-domain solution
1. Cross-domain via jsonp
2. Document.domain iframe cross-domain
3. Location.hash iframe
4. Window.name iframe cross-domain
5. postMessage cross-domain
6. Cross-domain resource sharing (CORS)
7. nginx proxy cross-domain
8. nodejs middleware proxy cross-domain
9. WebSocket protocol cross-domain
1. Cross-domain through jsonp
Usually in order to reduce the load on the web server, we separate static resources such as js, css, img, etc. to another server with an independent domain name, and then use the corresponding tags in the html page from Loading static resources under different domain names is allowed by the browser. Based on this principle, we can dynamically create a script and then request a URL with parameters to achieve cross-domain communication.
1.) Native implementation:

<script> 
  var script = document.createElement(&#39;script&#39;); 
script.type = &#39;text/javascript&#39;; // 传参并指定回调执行函数为onBack script.src = &#39;http://www.domain2.com:8080/login?user=admin&callback=onBack&#39;; document.head.appendChild(script); // 回调执行函数 function onBack(res) {
 alert(JSON.stringify(res)); 
} </script>
Copy after login

The server returns as follows (the global function is executed when returning):

onBack({"status": true, "user": "admin"})
2.)jquery ajax:
$.ajax({ url: &#39;http://www.domain2.com:8080/login&#39;, type: &#39;get&#39;, dataType: &#39;jsonp&#39;, // 请求方式为jsonp jsonpCallback: "onBack", // 自定义回调函数名 data: {}});
Copy after login
3.)vue.js:
this.$http.jsonp(&#39;http://www.domain2.com:8080/login&#39;, { params: {}, jsonp: &#39;onBack&#39;}).then((res) => { console.log(res); })
Copy after login

Backend node.js code example:

var querystring = require(&#39;querystring&#39;);var http = require(&#39;http&#39;);var server = http.createServer();
server.on(&#39;request&#39;, function(req, res) {var params = qs.parse(req.url.split(&#39;?&#39;)[1]); 
var fn = params.callback; // jsonp返回设置 res.writeHead(200, { &#39;Content-Type&#39;: &#39;text/javascript&#39; }); 
res.write(fn + &#39;(&#39; + JSON.stringify(params) + &#39;)&#39;); 
res.end();});
server.listen(&#39;8080&#39;);console.log(&#39;Server is running at port 8080...&#39;);
Copy after login

jsonp Disadvantages: Only one kind of get request can be implemented.
2. document.domain iframe cross-domain
This solution is limited to cross-domain application scenarios where the main domain is the same but the subdomains are different.
Implementation principle: Both pages forcefully set document.domain as the basic main domain through js, thus achieving the same domain.
1.) Parent window: (http://www.domain.com/a.html))

<iframe id="iframe" src="http://child.domain.com/b.html"></iframe><script> document.domain = &#39;domain.com&#39;; var user = &#39;admin&#39;;</script>
Copy after login

2.) Child window: ([http://child.domain.com/ b.html)

<script> document.domain = &#39;domain.com&#39;; // 获取父窗口中变量 alert(&#39;get js data from parent ---> &#39; + window.parent.user);</script>
Copy after login

3. location.hash iframe cross-domain
Implementation principle: a wants to communicate with b across domains, through the intermediate page c. For three pages, the location.hash of iframe is used to transfer values ​​between different domains, and direct js access is used to communicate between the same domains.
Specific implementation: A domain: a.html -> B domain: b.html -> A domain: c.html, different domains a and b can only communicate one-way through hash values, b and c are also different Domains can only communicate in one direction, but c and a are in the same domain, so c can access all objects on the a page through parent.parent.
1.) a.html: ([http://www.domain1.com/a.html)]

<iframe id="iframe" src="http://www.domain2.com/b.html" style="display:none;"></iframe><script> var iframe = document.getElementById(&#39;iframe&#39;); 
// 向b.html传hash值 setTimeout(function() {
 iframe.src = iframe.src + &#39;#user=admin&#39;; 
}, 1000);
Copy after login

// Open to the callback method function onCallback(res) of the same domain c.html ) { alert('data from c.html ---> ' res); }

2.) b.html: (http://www.domain2. com/b.html))