当前位置:网站首页>跨域概述,简单积累
跨域概述,简单积累
2022-06-24 09:46:00 【右眸Remnant】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、同源策略
提供浏览器的一种安全机制;请求的url地址必须和浏览器上的url同域,即:端口,协议,IP全部相同,如果任意一个不满足,则表示不同源;
如:从127.0.0.1:8000 请求 127.0.0.1:8001
当未进行处理的时候,浏览器会作出反应发生报错进行拦截。
二、CORS
CORS(跨域资源共享),由浏览器和服务器双方共同支持解决跨域问题。
CORS请求分为两种:简单请求和非简单请求
- 请求方法为:HEAD, GET,POST其中一种
- HTTP的头信息不超过:Accept, Accept-Language, Content-Language, Content-type
满足上述两种情况,则表示简单请求
简单请求:仅发送一次
非简单请求:包含两次请求,第一次为OPTIONS(预检请求),如果预检通过则发送第二次请求(真正的数据)
解决跨域的方式:
- 简单请求:
服务器设置响应头:Access-Control-Allow-Origin = '域名' 或 ‘*’
- 非简单请求:
在发送预检请求的时候,允许请求方式:Access-Control-Request-Method; 允许请求头:Access-Control-Request-Headers
实现原理:
当浏览器发现跨域请求的时候,如果是简单请求,会在请求头中添加Origin字段,表示来自哪个源;服务器接收到响应,会在响应头中添加Access-Control-Allow-Origin,指定域名或者通配;
三、Django中的跨域解决方案
1. 在返回结果中允许信息
data = {
"dafd": access_token}
response = HttpResponse(json.dumps(data))
# 处理简单请求
response["Access-Control-Allow-Origin"] = "*"
# 预检方式
response["Access-Control-Allow-Methods"] = "POST, GET"
response["Access-Control-Max-Age"] = "1000"
# 预检请求头
response["Access-Control-Allow-Headers"] = "*"
return response
2. 使用django-cors-headers
django-cors-headers时第三方依赖,使用pip下载,修改settings配置文件
pip install django-cors-headers
在setting.py中添加应用:
INSTALLED_APPS = [
...
'corsheaders',
...
]
添加中间件:
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
文件末尾添加:
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'*'
)
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
)
边栏推荐
- Uniapp implements the function of clicking to make a call
- CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
- 如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
- Phpstrom code formatting settings
- Role of message queuing
- 整理接口性能优化技巧,干掉慢代码
- 使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
- Machine learning - principal component analysis (PCA)
- numpy. logical_ or
- leetCode-面试题 01.05: 一次编辑
猜你喜欢

一群骷髅在飞canvas动画js特效

线程的六种状态

How to customize sharing links in wechat for H5 web pages

TP5 using post to receive array data times variable type error: solution to array error

4. classification management business development

How to manage massive network infrastructure?

p5.js实现的炫酷交互式动画js特效

Uniapp implements the function of clicking to make a call

自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏

leetCode-1823: 找出游戏的获胜者
随机推荐
PHP encapsulates a file upload class (supports single file and multiple file uploads)
How does home office manage the data center network infrastructure?
机器学习——感知机及K近邻
Machine learning perceptron and k-nearest neighbor
leetCode-1823: 找出游戏的获胜者
记录一下MySql update会锁定哪些范围的数据
Distributed | how to make "secret calls" with dble
The difference between static link library and dynamic link library
SQL sever基本数据类型详解
CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
Geogebra instance clock
How large and medium-sized enterprises build their own monitoring system
Cookie encryption 4 RPC method determines cookie encryption
涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
p5.js千纸鹤动画背景js特效
Cicflowmeter source code analysis and modification to meet requirements
牛客-TOP101-BM29
numpy. logical_ and()
PostgreSQL DBA quick start - source compilation and installation