当前位置:网站首页>同源与跨域
同源与跨域
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上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
边栏推荐
- 2019年全球半导体营收同比下滑12%,中国市场份额第一
- MVCC的底层原理
- Capacitance in series and in parallel and capacitance in series and balance resistance
- antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
- ES6--解构赋值
- Codeforces Round #810 (Div. 2)B.party(思维题)超详细题解
- 最新获得淘宝app商品详情原数据 的API
- uva1377
- 为什么需要第三方支付?
- An in-depth understanding of crystal oscillation circuit derived from xtalin pin and xtalout pin of single chip microcomputer
猜你喜欢

技术分享 | 接口自动化测试中,如何做断言验证?

Codeworks 5 questions per day (average 1500) - day 24

codeforces每日5题(均1500)-第二十四天

图解LeetCode——剑指 Offer II 115. 重建序列(难度:中等)

MVCC的底层原理

An in-depth understanding of crystal oscillation circuit derived from xtalin pin and xtalout pin of single chip microcomputer

Qtexttospeech class of QT realizes voice broadcast function

预处理与宏定义

How to run kevinchappell / FormBuilder

2022年,软件测试还能学吗?别学了,软件测试岗位饱和了...
随机推荐
22 year PMP test [Quanzhen agile test]
ES6 -- Application of expansion operator
办公自动化解决方案——DocuWare Cloud 将应用程序和流程迁移到云端的完整的解决方案
C191: password compilation
[pytorch series] detailed explanation of the torchvision image processing library of pytorch
Solve the problem of displaying the scroll bar when there is no data in the viewui table
[C # network application programming] Experiment 3: process management exercise
Qtexttospeech class of QT realizes voice broadcast function
C171: attendance system
es6删除对象的属性_ES6删除对象中的某个元素「建议收藏」
uva1377
#yy关于鱼的英文学习
Acwing 692. g bus count difference + prefix and
图解LeetCode——592. 分数加减运算(难度:中等)
Product Manager: check where there is an error prompt of "system exception" on the offline
Datepicker date selector in viewui compatible solution in ie11 browser
发布2年后涨价100美元,Meta Quest 2的逆生长
C# 后台GC 的前因后果
uva1421
[redis] redis penetration, avalanche and breakdown