Due to security considerations, Javascript is restricted by cross-origin access capabilities. But sometimes we want to do some reasonable cross-origin access things, what should we do? There are two types of situations:
1. Access between subdomains based on the same parent domain. For details, see taobao.com, jipiao.taobao.com, and promotion.taobao.com. They have the same parent domain taobao.com.
2. Access between pages based on different parent domains. See the following three domain domains: taobao.com, baidu.com, and sina.com.cn. They have different parent domains.
The cross-origin solutions are as follows:
Solution 1:Server Proxy
The page JS of Domain A needs to access the Link under Domain B to obtain data. This scheme creates A Proxy Program (possibly any server program such as ASP and servlet) on the server side of Domain ), the page JS of Domain A directly calls the Proxy program in this domain. The proxy program is responsible for sending the request to the link under Domain B and obtaining the data, finally, return the data to the page JS using the Proxy.
The access process is: JS under Domain A --> Proxy under Domain A --> Link under Domain B
Example:
Step 1:
Domain A: http://Jipiao.taobao.com/test.htm
Javascript script on the page:
The Code is as follows: