django-cors-headers不起作用
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'corsheaders',
'rest_framework',
'world',
'userManager',
'markPost',
'BasicServices',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
Run Code Online (Sandbox Code Playgroud)
一切都很正常,但没有奏效
这里我的回复标题
Cache-Control: max-age=0
Content-Type: text/html; charset=utf-8
Date: Tue, 20 Jan 2015 13:16:17 GMT
Expires: Tue, 20 Jan 2015 13:16:17 GMT
Last-Modified: Tue, 20 Jan 2015 13:16:17 GMT
Server: WSGIServer/0.1 Python/2.7.8
Set-Cookie: csrftoken=snXksqpljbCLW0eZ0EElFxKbiUkYIvK0; expires=Tue, 19-Jan-2016 13:16:17 GMT; Max-Age=31449600; Path=/
Vary: Cookie
X-Frame-Options: SAMEORIGIN
Run Code Online (Sandbox Code Playgroud) 我使用 django-cors-headers 3.1.1 来处理 Django 后端和 Javascript 前端应用程序之间的请求和响应。传输是不安全的(即http,而不是https)。
\n\n当本地托管时,一切正常。但在服务器上部署后,我不再看到 CORS 标头。
\n\n\n\n错误信息:
\n\nAccess to XMLHttpRequest at \'http://[HOST_IP]/api/assets/\' from origin \'http://my_custom_domain.eu\' has been blocked by CORS policy: Response to preflight request doesn\'t pass access control check: No \'Access-Control-Allow-Origin\' header is present on the requested resource.\nRun Code Online (Sandbox Code Playgroud)\n\n我的 nginx 配置如下:
\n\nserver {\n listen 80;\n server_name [HOST_IP];\n\n location / {\n include proxy_params;\n proxy_pass http://unix:/home/ubuntu/[path_to_app]/app.sock;\n\n add_header \'Access-Control-Allow-Origin\' \'*\';\n add_header \'Access-Control-Allow-Methods\' \'GET, PUT, OPTIONS\';\n add_header \'Access-Control-Allow-Headers\' \'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range\';\n add_header …Run Code Online (Sandbox Code Playgroud)