当前位置:网站首页>一文搞懂│什么是跨域?如何解决跨域?
一文搞懂│什么是跨域?如何解决跨域?
2022-07-15 17:58:00 【51CTO】
目录
什么是跨域
- 域: 是指浏览器不能执行其他网站的脚本
- 跨域: 它是由浏览器的 同源策略 造成的,是浏览器对
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,不需要修改任何代码,并且不会影响服务器性能
- jsonp请求
jsonp是服务器与客户端跨源通信的常用方法。最大特点就是简单适用,兼容性好兼容低版本IE,缺点是只支持get请求,不支持post请求- 原理时网页通过添加一个
<script>元素,向服务器请求json数据,服务器收到请求后,将数据放在一个指定名字的回调函数的参数位置传回来
- 后端语言代理
- 可以通过一种没有跨域限制的语言中转一下,通过后端语言去请求资源,然后再返回数据
- 比如
http://www.autofelix.cn需要调用http://api.autofelix.cn/userinfo去获取用户数据,因为子域名不同,会有跨域限制 - 可以先请求
http://www.autofelix.cn下的php文件,比如http://www.autofelix.cn/api.php,然后再通过该php文件返回数据
- 后端语言的设置
- 主要通过后端语言主动设置跨域请求,这里以
php作为案例
边栏推荐
- 每日一题·1252.奇数值单元格的数目·模拟优化
- EasyCVR视频调阅页面如何增加对应视频的云台控制?
- Lifecycle: the foundation of lifecycle aware components - jetpack series (1)
- 每日一题·735.行星碰撞·栈模拟
- VxWorks environment construction and learning
- 676.实现一个魔法字典·前缀树
- 394. String decoding · stack
- 开发那些事儿:如何解决RK芯片视频处理编解码耗时很长的问题?
- Umask calculates the default permissions for creating files and directories
- Torch in pytoch repeat_ Analysis of interleave() function
猜你喜欢

Li Mu hands on deep learning V2 anchor box and code implementation in target detection

C# Channel 简单实现消息队列的发布、订阅

307. Area and retrieval - array modifiable

Log in and operate the database with the command line

Qt(二)UI控件简介与 可选树状控件演示

Torch in pytoch Analysis of nonzero() function

Race · 6116 · calculate the value of Boolean binary tree · recursion

GeoServer complete tutorial

Face beating

Vscode default new directory overlap
随机推荐
Difference between shape parameter and scale parameter in probability density function
竞赛·6116·计算布尔二叉树的值·递归
Is it safe to open an account online now? Want to know which are the top ten securities dealers in China?
使用POI替换word中的特定字符/文字改进版
测试光流传感器速度特性
Matlab:搭建神经网络
307·区域和检索-数组可修改
(零六)Flask有手就行——配置静态文件
Unity iotar augmented reality tutorial
Race · 6116 · calculate the value of Boolean binary tree · recursion
406·根据身高重建队列·贪心
Blue whale configuration framework
C语言·前缀树实现
每日一题·1252.奇数值单元格的数目·模拟优化
Software architecture and design (IV) -- data flow architecture
Daily question · 1217 Playing chips · greed
R language uses the melt function of reshape2 package to transform dataframe data from wide table to long table, and dcast function processes the data processed by melt function from long table to wid
家居建材企业如何打造智慧供应链管理体系?数商云供应链系统深耕采购、物流多业务应用场景
Matlab:trainingoptions() explain training options in detail
If the scheduled task is based on the framework