我正在使用react和Django创建一个应用程序。当我使用django登录时,我在cookie中设置了令牌,但Django响应cookie未在浏览器中设置。我试图努力调试但不能。不知道我在哪里做错了。
Request URL:http://localhost:8000/login/
Request Method:POST
Status Code:200 OK
Remote Address:127.0.0.1:8000
Response Headers
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:3000
Content-Type:application/json
Date:Sun, 12 Feb 2017 13:32:30 GMT
Server:WSGIServer/0.1 Python/2.7.10
Set-Cookie:token=97547ba32cb8abcfe81b28c47ee5e3b8087b54ac; Path=/
Set-Cookie:sessionid=6twm2h9mad6q8k4w637ww25zt6l0ck2d; expires=Sun, 26-Feb-2017 13:32:30 GMT; httponly; Max-Age=1209600; Path=/
Vary:Cookie
X-Frame-Options:SAMEORIGIN
Request Headers
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Authorization:Token undefined
Connection:keep-alive
Content-Length:41
Content-Type:application/json;charset=UTF-8
Host:localhost:8000
Origin:http://localhost:3000
Referer:http://localhost:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
客户请求代码
export function adminSignIn({username, password}) {
return function (dispatch) {
axios
.post("http://localhost:8000/login/", {username, password})
.then((response) => {
dispatch({type: ADMIN, payload: response.data})
dispatch({type: HIDE_LOGGIN_MODAL, payload: response.data})
})
.catch((error) => {
console.log(error)
})
}
}
settings.py
"""
Django settings for server project.
Generated by 'django-admin startproject' using Django 1.8.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '83vwkomf5s1y_!jt#=_dlgv!v0t38yl!a80h#r0buor70$0y7='
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'corsheaders',
'inventory',
'home'
)
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',
'django.middleware.security.SecurityMiddleware',
)
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 40
}
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
# CSRF_COOKIE_NAME = "XSRF-TOKEN"
ROOT_URLCONF = 'server.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'server.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'bigbasket',
'USER': 'naresh',
'PASSWORD': 'naresh',
'HOST': 'localhost'
# 'PORT': '',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
完成此请求后,浏览器中没有保存cookie。需要一些帮助。
答案 0 :(得分:1)
找到解决方案未发送withCredentials: true
谢谢@sideshowbarker
答案 1 :(得分:0)
在Axios帖子中添加axios.post('http://example.com',null,{withCredentials:true})