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
https://www.cnblogs.com/liuqingzheng/articles/9794285.html?tdsourcetag=s_pcqq_aiomsg