Django 与 Vue交互跨域问题解决

Django 与 Vue交互跨域问题解决

报错问题

Access to XMLHttpRequest at 'http://127.0.0.1:8000/apps03/student6/' from origin 'http://172.16.2.199:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

解决方法

pip install django-cors-headers

django 配置文件设置

ALLOWED_HOSTS = ["*"]

INSTALLED_APPS = [
	......
    'corsheaders',
]

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

CORS_ORIGIN_WHITELIST = (
    'http://172.16.2.199:8080',
)

CORS_ALLOW_CREDENTIALS = True

参考地址

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

posted @ 2020-12-14 14:43  helloord  阅读(340)  评论(0编辑  收藏  举报