跨域请求被阻止-使用Axios和Django REST
问题描述:
我正在尝试在节点服务器上使用本地服务(端口8080)的应用程序中发出GET请求.我正在使用Axios向同时也在本地(端口8000)提供服务的django REST服务器发出请求.
I am trying to make a GET request in my application that is being served locally (port 8080) using on a node server. I am using Axios to make the request to a django REST server that is also being served locally (port 8000).
我的请求如下:
axios.get('http://127.0.0.1:8000/recipes/',{headers: {"Access-Control-Allow-Origin": "*"}})
在Django方面,我已将它们包含在我的中间件中
On the Django side, I've included these in my middleware
MIDDLEWARE = [
'django.middleware.common.CommonMiddleware',
'corsheaders.middleware.CorsMiddleware',
]
,并且在我已安装的应用中:
and this in my installed apps:
INSTALLED_APPS = [
'corsheaders',
]
并包含此设置:
CORS_ORIGIN_ALLOW_ALL = True
但是我仍然收到CORS错误:
But I'm still getting a CORS error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:8000/recipes/. (Reason: missing token ‘access-control-allow-origin’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel).
知道我缺少什么吗?
答
我知道了.
我需要删除:
{headers: {"Access-Control-Allow-Origin": "*"}
从请求中输入
.显然,标头应仅是响应的一部分.
from the request. Apparently that header should only be part of the response.
删除后,一切正常.