zoukankan      html  css  js  c++  java
  • cors中间件

    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"
    
            return response

    在settings.py文件中添加中间件

    前后端分离开发

     或导入

    from django.utils.deprecation import MiddlewareMixin

    查找MiddlewareMixin方法:1 .在setting.py文件中,随便找一个中间件

                  2.查看父类

                  3.查看父类所在.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'] = "*"
            
            if request.method == "OPTIONS":
                # 允许你携带Content-Type请求头
                response['Access-Control-Allow-Headers'] = "Content-Type"
                # 允许你发送DELETE,PUT
                response['Access-Control-Allow-Methods'] = "DELETE,PUT"
    
            return response
  • 相关阅读:
    (转)simple-framework(MaliSDK框架分析)
    (转)libhybris及EGL Platform-在Glibc生态中重用Android的驱动
    (原)在firefly_rk3288开发板上解决openGL在设置32位色深以后出现花屏的问题
    参考论坛:Mali kernel driver TX011-SW-99002-r5p1-00rel0 for firefly
    (转)android媒体--stagefright概述【一】
    (转)android系统架构及源码目录结构
    (原)linux下利用cmake来编译jthread开源库
    (原)U盘可见容量不能被识别的处理方法
    学会阅读Java字节码
    JVM
  • 原文地址:https://www.cnblogs.com/wt7018/p/11531343.html
Copyright © 2011-2022 走看看