Home > Article > Web Front-end > How to understand Js cross-domain
What is cross-domain
The browser has the same origin policy and does not allow ajax to access the interface of other domains
Cross-domain conditions: protocol, domain name, port, if there is one difference, it is considered cross-domain
http The default port is 80
https The default port is 443
There are three tags that allow cross-domain loading of resources
65179b6b460f0bc47f88d742253d467f // Can be used for statistics. The statistics website may be from other domains
cf63513d015db02810a3e91eb1a177962cacc6d41bbb37262a98f745aa00fbf0 // Can be used for JSONP, as well as Use CDN
c7c25c3ec3d204f0cd21685269e19de7 // You can use CDN, CDN is also used in other domains
Several ways to cross domains
1. JSONP cross-domain data request
JSONP implementation principle
1. Load http://www.baidu.com/test.html
2. Not necessarily served There is really a test.html file on the end
3. The server can dynamically generate a test.html file according to the request and return
4. The same principle applies to 84a8923902981e74e8ada5c80e665fc22cacc6d41bbb37262a98f745aa00fbf0
##
<script> window.callback = function (data) { console.log(data); // 这是跨域得到的信息 } </script> <script src="http://www.baidu.com/api.js"></script> <!-- api.js 内容是: callback({x: 100, y: 200}) -->2. Set http header on the server. This is a way to solve cross-domain problems in the future. Trend
// 注意:不同后端语言的写法可能不一样 // 第二个参数填写允许跨域的域名,* 代表允许所有域,不建议直接写 * response.setHeader("Access-Control-Allow-Origin", "http://a.com, http://b.com");
The above is the detailed content of How to understand Js cross-domain. For more information, please follow other related articles on the PHP Chinese website!