cors头以启用django服务器上的cors。我的
setting.py
就像这样。
CORS_ORIGIN_ALLOW_ALL= True
CORS_ALLOW_CREDENTIALS = True
我还补充了
corsheaders
到
INSTALLED_APPS
还有我的
MIDDLEWARE
就像这样
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
]是的。
我设置了一个非常简单的视图,看起来像这样。
def get(self, request):
response = HttpResponse("hi")
response['Set-Cookie'] = ('food=bread; Path=/; max_age=10000')
print(response._headers)
return response
控制台上的标题是这样的。
{'set-cookie': ('Set-Cookie', 'food=bread; drink=water; Path=/; max_age=10000'), 'content-type': ('Content-Type', 'text/html; charset=utf-8')}
当我在浏览器中调用我的API时,Cookie被设置,所有的东西都可以,但是当我在响应体中使用Ajax的AXIOS时,没有什么类似于我的Cookie。
我的javasctip代码我喜欢这样。
axios.get('http://37.130.202.188:13434/users/test/',
{withCredentials: true,
})
.then(function (response) {
console.log(response);
});
我用这个命令运行我的服务器
python manage.py runserver
对这种头痛的每一个反应都将非常感激。