当前位置:网站首页>Understand │ what is cross domain? How to solve cross domain problems?
Understand │ what is cross domain? How to solve cross domain problems?
2022-07-19 04:01:00 【Geek flying rabbit】
What is cross-domain
- Domain : It means that the browser cannot execute scripts of other websites
- Cross domain : It's run by the browser The same-origin policy Caused by the , It 's the browser
JavaScript
Security restrictions imposed , The so-called homology ( I mean in the same domain ) Two pages have the same protocolprotocol
, hosthost
And port numberport
Will cause Cross domain
Cross domain scenario
- What are the cross domain scenarios of the scenario , Please refer to the table below
At present url | request url | Cross domain or not | reason |
---|---|---|---|
http://www.autofelix.cn | http://www.autofelix.cn/api.php | no | agreement / domain name / Ports are the same |
http://www.autofelix.cn | https://www.autofelix.cn/api.php | yes | Different agreements |
http://www.autofelix.cn | http://www.rabbit.cn | yes | The main domain name is different |
http://www.autofelix.cn | http://api.autofelix.cn | yes | Different subdomains |
http://www.autofelix.cn:80 | http://www.autofelix.cn:8080 | yes | Different ports |
There are four ways to solve cross domain problems
- nginx Reverse proxy of
- Use
nginx
Reverse proxy implementation across domains , It's the easiest way to cross domains - It just needs to be modified
nginx
Is configured to solve cross-domain problems , Support for all browsers , Supportsession
, You don't need to change any code , It does not affect server performance
// nginx To configure
server {
listen 81;
server_name www.domain1.com;
location / {
proxy_pass http://www.domain2.com:8080; # Reverse proxy
proxy_cookie_domain www.domain2.com www.domain1.com; # modify cookie In the domain name
index index.html index.htm;
# When used webpack-dev-server Such as middleware proxy interface access nignx when , No browser is involved at this time , So there is no homologous restriction , The following cross-domain configuration is not enabled
add_header Access-Control-Allow-Origin http://www.domain1.com; # The current end is cross-domain only cookie when , for *
add_header Access-Control-Allow-Credentials true;
}
}
- jsonp request
jsonp
It is a common method of cross source communication between server and client . The biggest characteristic is simple and applicable , Compatibility is good.Compatible with lower versions IE
, The disadvantage is that it only supportsget
request , I won't support itpost
request- The principle of web pages is to add a
<script>
Elements , Request... From the serverjson
data , After the server receives the request , Put the data in the parameter position of a callback function with a specified name and pass it back
//jquery Realization
<script>
$.getJSON('http://autofelix.com/api.php&callback=?', function(res) {
// Process the data obtained
console.log(res)
});
</script>
- Back end language agent
- You can transfer it through a language without cross domain restrictions , Request resources through the back-end language , And then it returns the data
- such as
http://www.autofelix.cn
Need to callhttp://api.autofelix.cn/userinfo
To get user data , Because the subdomain name is different , There will be cross domain restrictions - You can ask first
http://www.autofelix.cn
Under thephp
file , such ashttp://www.autofelix.cn/api.php
, And then through thatphp
The file returns data
// api.php Code in file
public function getCurl($url, $timeout = 5)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = getCurl('http://api.autofelix.cn/userinfo');
return $result;
- Back end language settings
- It is mainly through the back-end language to actively set cross domain requests , Here we use
php
As a case study
// Allow access to all domain names
header('Access-Control-Allow-Origin: *');
// Allow a single domain name to access
header('Access-Control-Allow-Origin: https://autofelix.com');
// Allow multiple custom domain names to access
static public $originarr = [
'https://autofelix.com',
'https://baidu.com',
'https://csdn.net',
];
// Get the current cross domain domain name
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
if (in_array($origin, self::$originarr)) {
// allow $originarr In the array Domain name cross domain access
header('Access-Control-Allow-Origin:' . $origin);
// Response type
header('Access-Control-Allow-Methods:POST,GET');
// belt cookie Cross domain access to
header('Access-Control-Allow-Credentials: true');
// Response header Settings
header('Access-Control-Allow-Headers:x-requested-with,Content-Type,X-CSRF-Token');
}
边栏推荐
- Tutorial: Adaptive Replication and Partitioning in Data Systems
- 1255. Dynamic programming for state compression of the word set with the highest score
- A Tutorial on Learned Multi-dimensional Indexes
- EfficientNet系列(1): EfficientNetV2网络详解
- Nature Communications
- Hcip seventh day notes
- Chapter 4 user data analysis
- Common table expression CTE in Clickhouse
- Redis data migration: Method 1 RDB
- A robust deformation convolution neural network image denoising
猜你喜欢
[C language] 0 basic tutorial - file operation (to be continued)
Penetration test-02 vulnerability scanning
HCIP第五天笔记
Asp.NetCore 中使用grpc
A Tutorial on Learned Multi-dimensional Indexes
StyleFlow简明阅读:使用持续流完成属性编辑
Leetcode 931: minimum sum of descent path
The adaptation of go language under windows10:vscode
Timeline components
Use flink1.14 to operate iceberg0.13
随机推荐
[MySQL] install and configure MySQL on the ECS and connect with idea
Branches and tools of Orthodontics
要想组建敏捷团队,这些方法不可少
Workload-Aware Performance Tuning for Autonomous DBMSs
I'm 25 years old, but I still can't do anything. What if I can't find a way out? How about we media?
HCIP第八天笔记
针孔微创牙龈手术(Pinhole Gum Rejuvenation)
剑指 Offer 59 - II. 队列的最大值
修改jar包内容
The software supply chain security risk that makes enterprise digitalization and it executives bear the brunt of the cauldron points to the north
Enterprises love hybrid app development, and applet container technology can improve efficiency by 100%
Redis数据迁移方法四
Workload-Aware Performance Tuning for Autonomous DBMSs
Redis data migration method III
automake中文手册_incomplete
自然语言处理 知识点积累
The biggest bug I've ever written in my career as a programmer! Netizen: high and low is a P8 level!
Modify jar package content
Shorten the line width of tabs component
树莓派配置