一: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. 【LEETCODE】59、数组分类,适中级别,题目:39、48、64

    package y2019.Algorithm.array.medium; import java.util.*; /** * @ProjectName: cutter-point * @Packag ...

  2. 字符串类型日期时间转换为Date类型解析转换异常java.text.ParseException: Unparseable date: “2019-09-27T18:31:31+08:00”

    错误的写法: SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //这里的格式也可以是别 ...

  3. Failed to transfer file: http://repo.maven.apache.org/maven2/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar

    解决办法:maven的配置文件settings.xml中添加mirror地址 <mirror>       <id>alimaven</id>       < ...

  4. Spring Boot(二)

    Spring MVC流程图 注册流程图: result代码: import java.io.UnsupportedEncodingException; import java.net.URLEncod ...

  5. SpringBoot +MSSQL

    ____SpringBoot +MSSQL_______________________________________________________________________________ ...

  6. Navicat 连接mysql 报错: Authentication plugin caching_ sha2_password cannot be loaded

    出现这个错误的时候, 网上的资料都是修改mysql的登录信息的, ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password ...

  7. android 常用库的地址--dialog,recycler

    android 弹出框     https://github.com/li-xiaojun/XPopup android  RecyclerViewAdapter     https://github ...

  8. YII 的SPA 写法

    'use strict'; var findToolbar = function () { return document.querySelector('#yii-debug-toolbar'); } ...

  9. 【转载】 C#使用string.IsNullOrWhiteSpace方法判断字符串是否为非空字符

    在C#编程过程中,很多时候需要判断传入过来的字符串是否为Null或者空字符或者空白字符,此时就可以使用到string.IsNullOrWhiteSpace方法来判断,如果字符串为null或者空字符Em ...

  10. python day7: time,datetime,sys,pickle,json模块

    目录 python day 7 1. time模块 2. datetime模块 2.1 date类 2.2 time类 2.3 datetime类 2.4 timedelta类 2.5 tzinfo时 ...