当前位置:网站首页>同源与跨域
同源与跨域
2022-07-27 17:50:00 【小猪不笨】
一、同源策略
1、同源(origin)
- 同源:若地址里面的协议、域名和端口号均相同则属于同源。
- 以下是相对于 http://www.a.com:8080/test/index.html 的同源检测:
http://www.a.com/dir/page.html ----成功(http默认端口号位8080)
http://www.child.a.com/test/index.html ----失败,域名不同
https://www.a.com/test/index.html ----失败,协议不同
http://www.a.com:8089/test/index.html ----失败,端口号不同
2、同源策略
- 同源策略是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源。所以a.com下的js脚本采用ajax读取b.com里面的文件数据是会报错的。
- 不受同源策略限制的情况:
①页面中的链接,重定向以及表单提交是不会受到同源策略限制的。
②跨域资源的引入是可以的。但是js不能读写加载的内容。如嵌入到页面中的<script src="..."></script>,<img>,<link>,<iframe>等。
二、跨域
1.跨域:
只要协议、域名、端口号有一个不同就是跨域。
2.跨域的原因:
跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号(如存在)相同,则允许相互访问。为了防止某域名下的接口被其他域名下的网页非法调用,是浏览器对JavaScript施加的安全限制。也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源。跨域问题是针对JS和ajax的,html本身没有跨域问题,比如a标签、script标签、甚至form标签(可以直接跨域发送数据并接收数据)等。
三、跨域问题解决方案
1、jsonp
利用script标签可跨域的特点,在跨域脚本中可以直接回调当前脚本的函数。
2、cors
服务器设置HTTP响应头中Access-Control-Allow-Origin值,解除跨域限制。
注意:这两个跨域方案都存在一个致命的缺陷,严重依赖后端的协助。
3、反向代理(Reverse Proxy):
前端独立就能解决的跨域方案。
指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
边栏推荐
- 办公自动化解决方案——DocuWare Cloud 将应用程序和流程迁移到云端的完整的解决方案
- 2022 love analysis · smart community manufacturer panoramic report manufacturer solicitation
- LED high precision scale scheme specification
- Use cpolar to build a business website (5)
- [pytorch series] detailed explanation of the torchvision image processing library of pytorch
- Clickhouse 实现 MaterializedPostgreSQL
- 22 year PMP test [Quanzhen agile test]
- ‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
- 汇顶科技:收购恩智浦VAS业务已完成交割
- If you want to switch to software testing, you should pass these three tests first, including a 3000 word super full test learning guide
猜你喜欢

连接池-归还连接详解(上)

发布2年后涨价100美元,Meta Quest 2的逆生长

If you want to switch to software testing, you should pass these three tests first, including a 3000 word super full test learning guide

Use cpolar to build a business website (5)

New library online | cnopendata detailed address data of all patents in China

'vite' is not an internal or external command, nor is it a runnable program or batch file
![22 year PMP test [Quanzhen agile test]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
22 year PMP test [Quanzhen agile test]

unity2D 动态漫画剧本(给猛虎桥章节做动画演示二)

Unity2d dynamic cartoon script (animation demonstration II for the chapter of Tiger Bridge)

想转行软件测试,先过这三关,包含一份3000字超全测试学习指南
随机推荐
uva1377
shell
MongoDB 学习笔记: BSON 结构分析
JS array method foreach and map comparison
想转行软件测试,先过这三关,包含一份3000字超全测试学习指南
长安链数据存储源码分析
Capacitance in series and in parallel and capacitance in series and balance resistance
内置函数时间日期函数
PMP practice once a day | don't get lost in the exam -7.27 (including agility + multiple choices)
连接池-归还连接详解(上)
LG Group announced that it would donate 3million yuan in cash, 1.2 million masks and 10000 sets of protective clothing to Hubei
To share the denoising methods and skills of redshift renderer, you must have a look
ZJNU 22-07-26 比赛心得
Connection pool - return connection details (Part 1)
如何快速提升抖音小店三分钟回复率?哪些情况会影响抖音小店回复率呢?
康佳首批10万颗存储主控芯片售罄,2020年预计销量1亿颗
LeetCode练习2——两数之和
uva1421
PyQt5快速开发与实战 4.3 QLabel and 4.4 文本框类控件
Unified Modeling Language (UML) specification