drf解决跨域问题 使用 django-corse-headers扩展

跨域CORS

使用django-corse-headers扩展

安装

pip install django-cors-headers

添加应用

INSTALLED_APPS = (
    ...
    'corsheaders',
    ...
)

中间层设置

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    ...
]

添加白名单

# CORS
CORS_ORIGIN_WHITELIST = (
    '127.0.0.1:8080',
    'localhost:8080',
)
CORS_ALLOW_CREDENTIALS = True # 允许携带cookie

猜你喜欢

转载自www.cnblogs.com/a-way-blog/p/9847692.html