Vue.js 使用Axios向DRF发送POST
我正试图使用Vue.js 使用Axios向DRF发送POST,vue.js,django-rest-framework,axios,Vue.js,Django Rest Framework,Axios,我正试图使用vuejs中的axios库发出一个简单的POST请求,但由于某种原因,DRF没有收到参数。当通过Postman发出相同请求时,它将接收参数 VueJS postLogin(credentials){ return axios({ method: "POST", url: "company/login/", data: credentials, }).catch(err => { return TreatErrors
vuejs
中的axios
库发出一个简单的POST
请求,但由于某种原因,DRF
没有收到参数。当通过Postman
发出相同请求时,它将接收参数
VueJS
postLogin(credentials){
return axios({
method: "POST",
url: "company/login/",
data: credentials,
}).catch(err => {
return TreatErrors.treatDefaultError(err);
})
}
DRF
@action(methods=['post'], detail=False)
# Debug set here shows POST comes empty from vuejs
def login(self, request, pk=None):
if not request.POST.get('email'):
raise ValidationError({
'message': 'You must provide an email'
})
使用Chrome DevTools
我可以清楚地看到参数被发送到DRF
我尝试过的
我尝试过从Postman
处理每个标题
,并将其粘贴到axios
中,但没有任何运气
postLogin(credentials){
return axios({
method: "POST",
url: "company/login/",
data: credentials,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).catch(err => {
return TreatErrors.treatDefaultError(err);
})
}
基本上,我访问数据的方式是错误的: 由此:
if not request.POST.get('email'):
raise ValidationError({
'message': 'You must provide an email'
})
对此
data = request.data
if not data.get('email'):
raise ValidationError({
'message': 'You must provide an email'
})
非常感谢你!上帝保佑!:)