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
// Can be used for statistics. The statistics website may be from other domains
// Can be used for JSONP, as well as Use CDN
// 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
##
<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}) -->
// 注意:不同后端语言的写法可能不一样 // 第二个参数填写允许跨域的域名,* 代表允许所有域,不建议直接写 * 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!