Home > Web Front-end > JS Tutorial > body text

How to understand Js cross-domain

一个新手
Release: 2017-10-17 09:38:00
Original
1449 people have browsed it

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}) -->
Copy after login

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");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
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!