当前位置:网站首页>同源与跨域
同源与跨域
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上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。
边栏推荐
- Ten year test old bird talk about mobile terminal compatibility test
- Use cpolar to build a business website (5)
- uva1421
- Underlying principle of mvcc
- Function priority
- 'vite' is not an internal or external command, nor is it a runnable program or batch file
- es6删除对象的属性_ES6删除对象中的某个元素「建议收藏」
- 华为全联接大会2022开启曼谷之旅;Facebook推视频收入分成功能,创作者可获20%分成…
- Product Manager: check where there is an error prompt of "system exception" on the offline
- Assignment 1 - Hello World ! - Simple thread Creation
猜你喜欢

长安链数据存储源码分析

Datepicker date selector in viewui compatible solution in ie11 browser

Ms721 load test

邬贺铨:因地制宜 数字化技术赋能“双碳”实践

Product Manager: check where there is an error prompt of "system exception" on the offline

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

To share the denoising methods and skills of redshift renderer, you must have a look

Underlying principle of mvcc

一看就懂的ESLint

Can software testing be learned in 2022? Don't learn, software testing positions are saturated
随机推荐
[map set]
ES6--解构赋值
2019年全球半导体市场收入4183亿美元,同比下滑11.9%
产品经理:排查下线上哪里冒出个“系统异常”的错误提示
Ms721 load test
Program design Comprehensive Experiment III
PKI/TLS 工具之CFSSL —— 筑梦之路
内置函数其它函数
[C # network application programming] Experiment 3: process management exercise
速卖通:按关键字搜索商品 API
How to quickly improve the three minute response rate of Tiktok store? What will affect the reply rate of Tiktok store?
什么是多层感知机(什么是多层感知机)
Qtexttospeech class of QT realizes voice broadcast function
uva1421
PyQt5快速开发与实战 4.5 按钮类控件 and 4.6 QComboBox(下拉列表框)
Introduction to basic cesium controls
TS2532: Object is possibly ‘undefined‘
[openbmc series] 4. Start the process and use qume to simulate ast2600 EVB
antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key
PC Museum (3) MITs Altair 8800