解决前后端跨域问题
1.代码
from django.utils.deprecation import MiddlewareMixin
class CoresMiddleware(MiddlewareMixin):
def process_response(self, request, response):
response['Access-Control-Allow-Origin'] = "*"
response['Access-Control-Allow-Headers'] = "*"
response['Access-Control-Allow-Methods'] = "*"
return response
2.注册中间件
注意:
请求头不支持带下划线,要用驼峰;
跨域中间件要放首位(可以试一下)