1、在 App目录下新建一个 middlewares.py文件
在里面输入
# 解决跨域请求
from django.utils.deprecationimport MiddlewareMixin
class MyTest(MiddlewareMixin):
def process_response(self, request, response):
response['Access-Control-Allow-Origin']= "*"
return response
2、在 setting.py 的MIDDLEWARE属性的最后一行输入
# 解决跨域请求
'PhotoSelectorApp.middlewares.MyTest',
MIDDLEWARE属性的全部内容如下:
MIDDLEWARE= [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# 解决跨域请求
'PhotoSelectorApp.middlewares.MyTest',
]
3、之后重启项目即可。