一:settings 中间件配置路径

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'app01.cors.CORSMiddleware', # 文件路径
]
# 表示所有的ip 都能访问
ALLOWED_HOSTS = ['*',]
 

二:cors.py

class MiddlewareMixin(object):
def __init__(self, get_response=None):
self.get_response = get_response
super(MiddlewareMixin, self).__init__() def __call__(self, request):
response = None
if hasattr(self, 'process_request'):
response = self.process_request(request)
if not response:
response = self.get_response(request)
if hasattr(self, 'process_response'):
response = self.process_response(request, response)
return response class CORSMiddleware(MiddlewareMixin): def process_response(self,request,response):
# 添加响应头 # 允许你的域名来获取我的数据
response['Access-Control-Allow-Origin'] = "*" # 允许你携带Content-Type请求头
# response['Access-Control-Allow-Headers'] = "Content-Type" # 允许你发送DELETE,PUT
# response['Access-Control-Allow-Methods'] = "DELETE,PUT" # 预检请求 -- 登陆的跨域
if request.method == 'OPTIONS':
response['Access-Control-Allow-Headers'] = 'Content-Type'
# 需要什么类型的请求头就在后面直接添加,不能加*
response['Access-Control-Allow-Methods'] = 'PUT,DELETE' return response

python - Django 跨域配置的更多相关文章

  1. Django+Vue跨域配置与经验

    一.原理 同源?同源策略? 同源的定义是:两个页面的协议.端口和域名都相同 同源的例子: 不同源的例子: 同源策略SOP(Same origin policy)是一种浏览器约定,它是浏览器最核心也最基 ...

  2. python Josnp(跨域)

    python Josnp(跨域) 所谓的跨域就是进行不用域名内的请求,好比如说我在A域名想访问B域名的内容就是一种跨域的行为. 但是在我们浏览器端会有一个同源策略的设置,这个同源策略只对Ajax请求有 ...

  3. Django跨域请求之JSONP和CORS

    现在来新建一个Django项目server01,url配置为 url(r'^getData.html$',views.get_data) 其对应的视图函数为get_data: from django. ...

  4. Django跨域问题(CORS错误)

    Django跨域问题(CORS错误) 一.出现跨域问题(cors错误)的原因 通常情况下,A网页访问B服务器资源时,不满足以下三个条件其一就是跨域访问 协议不同 端口不同 主机不同 二.Django解 ...

  5. Django跨域:

    下包 pip install django-cors-headers 下面的操作在setting里 添加到appps里 INSTALLED_APPS = [ ... 'corsheaders', .. ...

  6. Asp.Net Core跨域配置

    在没有设置跨域配置的时候,Ajax请求时会报以下错误 已拦截跨源请求:同源策略禁止读取位于 http://localhost:5000/Home/gettime 的远程资源.(原因:CORS 头缺少 ...

  7. Springboot统一跨域配置

    前言:跨域是什么? 要知道跨域的概念,我们先明确怎样算是同一个域: 同一个域指的是同一协议,同一ip,同一端口 如果这三同中有一者不同就产生了跨域. 在做前后端分离的项目中,通过ajax请求后台端口时 ...

  8. Asp.net跨域配置

    <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...

  9. asp.net (webapi) core 2.1 跨域配置

    原文:asp.net (webapi) core 2.1 跨域配置 官方文档 ➡️ https://docs.microsoft.com/zh-cn/aspnet/core/security/cors ...

随机推荐

  1. CRT远程连接centos7,连接超时

    CRT远程连接centos7,连接超时 问题原因: 宿主机(win10)和虚拟机(centos7)不在同一个网段 在宿主机无法ping通虚拟机, 首先在cmd窗口ipconfig查看一下vmnet的i ...

  2. el-input 标签中密码的显示和隐藏

    效果展示: 密码隐藏: 密码显示: 代码展示: 一:<el-input>标签代码 <el-form-item label="密码" prop="pass ...

  3. 19 IO流(十六)——Commons工具包,FileUtils(一)

    Commons包的API:自己查吧懒得传云 Commons包的导入方法 Commons是一个java的IO开源工具,导入方法: 从apache.org下载commons包 解压 copy其中的comm ...

  4. 网络 TCP三次握手,四次挥手详解

    三次握手,四次挥手可以说是炙手可热的面试题了,来看看它究竟长什么样子吧! 我们先把流程图贴上来 : 为什么这么复杂? 因为TCP是可靠性传输. 确认可靠传输的前提:  TCP连接管理机制 用TCP首部 ...

  5. M-SOLUTIONS Programming Contest

    A.(n-2)*180 #include<cstdio> #include<cstring> #include<iostream> #include<algo ...

  6. Shell变量一览

    Shell变量一览 $# Shell命令的参数个数 $$ Shell本身的进程ID $! Shell最后运行的后台进程的进程ID $? Shell最后运行的命令的退出码(返回值) $- Shell使用 ...

  7. Linux用户组笔记整理

    一.Linux用户组概念 Linux用户组(group)就是具有相同操作权限范围的Linux用户管理起来: 比如有时我们要让同一类用户具有相同的权限,比如查看.修改某一文件或执行某个命令, 这时我们需 ...

  8. 私有属性和私有方法l

    class Woman: def __init__(self, name): self.name=name self.__age=18 def __secret(self): print(" ...

  9. AppRTC服务搭建(测试)

    提供一个在线的webrtc服务器测试,需要的朋友看看.https://www.webrtcserver.cn/ 服务器搭建环境各有不同在此参考前人经验差试一下. 运行AppRTC需要使用Google ...

  10. Part_one:Redis第一次接触

    1.redis学习 数据默认写入到内存,如果断电,服务器宕机,redis进程挂掉,数据会丢失. selenium操作浏览器时,要注意浏览器资源释放,方式内存泄露,崩溃 mysql是文件型数据库,默认持 ...