django写的接口移动端可以调用,用web访问报错如下:
建一个中间件py文件,middlewares.py:
from django.utils.deprecation import MiddlewareMixin
class CrossDomainMiddleware(MiddlewareMixin):
def process_response(self, request, response):
#跨域中间件
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Headers'] = 'Content-Type'
return response
然后在settings.py的中间件MIDDLEWARE里进行引用:
blog.middlewares.CrossDomainMiddleware
注意路径,前面是项目名,中间是中间件py文件名,后面是middlewares.py里的方法名,我的middlewares.py文件路径如下
再去请求已经没有毛病了: