1.VUE上的请求:
安装qs: npm install qs --save
请求:
var qs = require('qs')
this.form.user = localStorage.getItem('loginname')
console.log('***********')
console.log(qs.stringify(this.form))
// this.form的值:user=editor&phone1=&phone2=&messagenameen=&messagenamezh=&groupid=
axios.post('http://localhost:8000' + '/usersmanage/getpersoninfo/', qs.stringify(this.form),
{
headers:
{
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).then((response) => {
if (response.status === 'success') {
console.log('yesyesyes')
} else {
this.$message({
message: '获取个人信息失败!',
type: 'error'
})
}
this.dialogFormVisible = false
}).catch(function(error) {
console.log(error)
})
2. Django上安装django-cors-headers 解决跨域请求问题
pip3 install django-cors-headers
然后配置:在settings.py中
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
#……
'corsheaders',
]
MIDDLEWARE = [
# ……
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
]
# 解决跨域请求问题
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = () #空白表示任意
CORS_ALLOW_METHODS = (
'DELETE',
'GET',
'OPTIONS',
'PATCH',
'POST',
'PUT',
'VIEW',
)
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
)