gpt4 book ai didi

vue.js - 使用Axios进行POST到DRF

转载 作者:行者123 更新时间:2023-12-03 06:48:57 28 4
gpt4 key购买 nike

我正在尝试使用POST内的axios库发出一个简单的vuejs请求,但由于某些原因,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
chrome dev tools

我尝试过的
我尝试过应对 Headers中的每个 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'
})

关于vue.js - 使用Axios进行POST到DRF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571827/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com