后台处理跨域


https://github.com/ottoyiu/django-cors-headers/

安装django-cors-headers模块

在settings.py中配置
# 注册app
INSTALLED_APPS = [
'corsheaders'
]
# 添加中间件
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware'
]

# 允许跨域源
CORS_ORIGIN_ALLOW_ALL = False
# 配置指定跨域域名
CORS_ORIGIN_WHITELIST = [
'http://example.com'
]

# 允许ajax请求携带cookie
CORS_ALLOW_CREDENTIALS = True


# 注:前台请求头携带参数,中间件拒绝Access-Control-Allow-Headers错误,中间件要设置 default_headers
from corsheaders import defaults
在 default_headers 中添加 '前端ajax请求头里面的字段名'

猜你喜欢

转载自www.cnblogs.com/huikejie/p/11397391.html