我已经使用bitnami在AWS上安装了Django(Shopify应用)。一切正常,但是当我从Shopify商店向Django发送xmlhttp请求时,出现CORS错误-
从源访问“ https://django-app-url”处的XMLHttpRequest “ https://store_url”已被CORS政策禁止:否 请求中存在“ Access-Control-Allow-Origin”标头 资源。
我在django上启用了cors,因为我之前在aws上使用openlite并可以正常工作。 转移到bitnami之后,我开始出现错误-
已安装cors标头-
pip3 install django-cors-headers
和在中间件和已安装的应用程序的settings.py中添加的cors标头
也在/opt/bitnami/apps/django/django_projects/Project/conf httpd.conf
Header set Access-Control-Allow-Origin "*"
答案 0 :(得分:0)
在您的settings.py中添加以下内容。希望它能工作。
我遇到同样的问题,添加了这些设置后,问题已得到解决!
CORS_ALLOW_CREDENTIALS = True
ACCESS_CONTROL_ALLOW_HEADERS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
'localhost:4200',
'localhost:8000',
'127.0.0.1:8000',
...
)
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
)
CSRF_TRUSTED_ORIGINS = (
'127.0.0.1:9200',
....
)
CORS_ALLOW_HEADERS = (
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
)