当前位置:网站首页>一文搞懂│什么是跨域?如何解决跨域?
一文搞懂│什么是跨域?如何解决跨域?
2022-07-17 01:58:00 【极客飞兔】
什么是跨域
- 域: 是指浏览器不能执行其他网站的脚本
- 跨域: 它是由浏览器的 同源策略 造成的,是浏览器对
JavaScript实施的安全限制,所谓同源(即指在同一个域)就是两个页面具有相同的协议protocol,主机host和端口号port则就会造成 跨域

跨域场景
- 场景的跨域场景有哪些,请参考下表
| 当前url | 请求url | 是否跨域 | 原因 |
|---|---|---|---|
http://www.autofelix.cn | http://www.autofelix.cn/api.php | 否 | 协议/域名/端口都相同 |
http://www.autofelix.cn | https://www.autofelix.cn/api.php | 是 | 协议不同 |
http://www.autofelix.cn | http://www.rabbit.cn | 是 | 主域名不同 |
http://www.autofelix.cn | http://api.autofelix.cn | 是 | 子域名不同 |
http://www.autofelix.cn:80 | http://www.autofelix.cn:8080 | 是 | 端口不同 |
解决跨域的四种方式
- nginx的反向代理
- 使用
nginx反向代理实现跨域,是最简单的跨域方式 - 只需要修改
nginx的配置即可解决跨域问题,支持所有浏览器,支持session,不需要修改任何代码,并且不会影响服务器性能
// nginx配置
server {
listen 81;
server_name www.domain1.com;
location / {
proxy_pass http://www.domain2.com:8080; #反向代理
proxy_cookie_domain www.domain2.com www.domain1.com; #修改cookie里域名
index index.html index.htm;
# 当用webpack-dev-server等中间件代理接口访问nignx时,此时无浏览器参与,故没有同源限制,下面的跨域配置可不启用
add_header Access-Control-Allow-Origin http://www.domain1.com; #当前端只跨域不带cookie时,可为*
add_header Access-Control-Allow-Credentials true;
}
}
- jsonp请求
jsonp是服务器与客户端跨源通信的常用方法。最大特点就是简单适用,兼容性好兼容低版本IE,缺点是只支持get请求,不支持post请求- 原理时网页通过添加一个
<script>元素,向服务器请求json数据,服务器收到请求后,将数据放在一个指定名字的回调函数的参数位置传回来
//jquery实现
<script>
$.getJSON('http://autofelix.com/api.php&callback=?', function(res) {
// 处理获得的数据
console.log(res)
});
</script>
- 后端语言代理
- 可以通过一种没有跨域限制的语言中转一下,通过后端语言去请求资源,然后再返回数据
- 比如
http://www.autofelix.cn需要调用http://api.autofelix.cn/userinfo去获取用户数据,因为子域名不同,会有跨域限制 - 可以先请求
http://www.autofelix.cn下的php文件,比如http://www.autofelix.cn/api.php,然后再通过该php文件返回数据
// api.php 文件中的代码
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;
- 后端语言的设置
- 主要通过后端语言主动设置跨域请求,这里以
php作为案例
// 允许所有域名访问
header('Access-Control-Allow-Origin: *');
// 允许单个域名访问
header('Access-Control-Allow-Origin: https://autofelix.com');
// 允许多个自定义域名访问
static public $originarr = [
'https://autofelix.com',
'https://baidu.com',
'https://csdn.net',
];
// 获取当前跨域域名
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
if (in_array($origin, self::$originarr)) {
// 允许 $originarr 数组内的 域名跨域访问
header('Access-Control-Allow-Origin:' . $origin);
// 响应类型
header('Access-Control-Allow-Methods:POST,GET');
// 带 cookie 的跨域访问
header('Access-Control-Allow-Credentials: true');
// 响应头设置
header('Access-Control-Allow-Headers:x-requested-with,Content-Type,X-CSRF-Token');
}
边栏推荐
- Chengxin University envi_ IDL second week class content: open hdf4 file and read the file, as well as simple data processing and saving + detailed analysis
- Analysis and processing of male and female voice signals based on MATLAB
- Go语言中的Iota关键字怎么使用
- Qt OpenGL 通过鼠标和键盘移动三维物体(设置相机)
- MySQL master-slave setup
- ResNet
- Vscode+ros2 environment configuration
- Thinkphp5.0模型操作使用page进行分页
- SparkCore核心设计:RDD,220716,
- The fourth day of the third question of daily Luogu
猜你喜欢

鼠标滑动两张图片前后对比js插件

线程的私有存储空间

Game theory of catching lice

Reptile learning (5): teach you reptile requests practice hand in hand

The installation software prompts that the program input point adddlldirectory cannot be located in the dynamic link library kernel32 DLL (download address at the end of the text)

支持工业级瘦设备4G接入,润和软件DAYU120通过OpenHarmony兼容性测评

代理模式——B站动力节点

論文閱讀:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
当 mysql 表从压缩表变成普通表会发生什么?

Nim博奔问题
随机推荐
数学建模比赛论文模板格式
No, check it out
Nim博奔问题
Edge detection method -- first order edge detection
leetcode 222. 完全二叉树的节点个数(必会)
SparkCore核心设计:RDD,220716,
ulsm配置案例
Latest installation tutorial of VMware Tools (rhel8)
Mouse slide two pictures before and after comparison JS plug-in
Browser cannot open tensorboard
XX市高中网络拓扑整体规划配置
leetcode:78. 子集
上班摸鱼打卡模拟器微信小程序源码
Install Net prompt "cannot establish a certificate chain to trust the root authority" (simple method with download address)
Theoretical basis of double Q-learning and its code implementation [pendulum-v0]
Number of supported question banks and examination question banks of swiftui examination question bank project (tutorial includes source code)
374. Guess the size of numbers (must be able to get started)
KubeCon + CloudNativeCon Europe 2022
Chapter 4 用户数据分析
Gnome boxes virtual machine creation and installation