Django----解决跨域

 

cors(跨域资源共享):

  本质设置响应头

 定制中间件 cors.py 后在settings.py中间件中配置

from django.utils.deprecation import MiddlewareMixin

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
from django.utils.deprecation import MiddlewareMixin


class CorsMiddleWare(MiddlewareMixin):
    def process_response(self, request, response):
        if request.method == 'OPTIONS':
            response['Access-Control-Allow-Headers'] = 'Content-Type'
            response['Access-Control-Allow-Method'] = 'POST,PUT,DELETE'

        response['Access-Control-Allow-Origin'] = '*'

        return response
l

 https://www.cnblogs.com/liuqingzheng/articles/9794285.html?tdsourcetag=s_pcqq_aiomsg

posted on 2018-11-03 11:40  <Hbw>  阅读(179)  评论(0编辑  收藏  举报