django-cors-headers
'''
瀏覽器具有 "同源策略的限制",導(dǎo)致 `發(fā)送ajax請(qǐng)求` + `跨域` 存在無法獲取數(shù)據(jù)。
- 簡單請(qǐng)求,發(fā)送一次請(qǐng)求倘待。
- 復(fù)雜請(qǐng)求,先options請(qǐng)求做預(yù)檢组贺,然后再發(fā)送真正請(qǐng)求
'''
1凸舵、使用pip安裝
pipinstall django-cors-headers
2、添加到setting的app中
INSTALLED_APPS = (
'corsheaders',
)
3失尖、添加中間件
MIDDLEWARE=['corsheaders.middleware.CorsMiddleware','django.middleware.common.CommonMiddleware',...]
4啊奄、setting下面添加下面的配置
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'*'
)
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
}