解决方案
1.安装django-cors-headers
pip install django-cors-headers
2.配置settings.py文件
INSTALLED_APPS = [...'corsheaders',...]MIDDLEWARE_CLASSES = (...'corsheaders.middleware.CorsMiddleware','django.middleware.common.CommonMiddleware', # 注意顺序...)# 跨域允许的请求方式,可以使用默认值,默认的请求方式为:# from corsheaders.defaults import default_methodsCORS_ALLOW_METHODS = ('GET','POST','PUT','PATCH','DELETE','OPTIONS')# 允许跨域的请求头,可以使用默认值,默认的请求头为:# from corsheaders.defaults import default_headers# CORS_ALLOW_HEADERS = default_headersCORS_ALLOW_HEADERS = ('XMLHttpRequest','X_FILENAME','accept-encoding','authorization','content-type','dnt','origin','user-agent','x-csrftoken','x-requested-with','Pragma',)# 跨域请求时,是否运行携带cookie,默认为FalseCORS_ALLOW_CREDENTIALS = True# 允许所有主机执行跨站点请求,默认为False# 如果没设置该参数,则必须设置白名单,运行部分白名单的主机才能执行跨站点请求CORS_ORIGIN_ALLOW_ALL = True
OK!问题解决!
