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 setting.py: 配置 MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', '......' 'api.cors.CORSMiddleware', ]