經過在服務器端引入cors解決跨域問題,造成和vue.js聯合使用vue
pip install django-cors-middlewaredjango
在 settings.py中添加,注意:不添加的話沒法生效跨域
INSTALLED_APPS = [ 'corsheaders', ]
MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware' ]
# 配置容許跨域訪問的域名 CORS_ORIGIN_ALLOW_ALL = False # 默認所有 CORS_ORIGIN_WHITELIST = () CORS_ORIGIN_REGEX_WHITELIST = () # 設置容許訪問的方法 CORS_ALLOW_METHODS = ('GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS') # 設置容許訪問的header CORS_ALLOW_HEADERS = ('x-requested-with', 'content-type', 'accept', 'origin', 'authorization', 'x-csrftoken')