首先需要定义一个中间件
from django.utils.deprecation import MiddlewareMixin class CorsMiddleware(MiddlewareMixin): def process_response(self,request,response): response["Access-Control-Allow-Headers"] = "Content-Type" #get请求 response["Access-Control-Allow-Origin"]="http://localhost:8081" #post请求 return response
还需要在setting中的MIDDLEWARE中注册中间件
"api.utils.middleware.CorsMiddleware"