【发布时间】:2019-11-22 16:06:01
【问题描述】:
我正在使用 Django/Python 构建一个 Web 应用程序,并尝试使用 Axios 库(在 Vue.js 代码中)通过 POST 请求将数据发送到控制器。
POST QueryDict 似乎是空的,我不明白为什么会这样:
changeCountry: function(e, id){
console.log("Let's change the country")
console.log(e.target.value) // is printing correctly
console.log(id) // also printing correctly
axios({
method: 'post',
url: '/template/country',
data: {
id: id,
country: e.target.value
},
headers: {
'X-CSRFToken': "{{csrf_token}}"
}
})
.then(function (response) {
alert(response.data); // this is returning what I expect
})
.catch(function (error) {
console.log(error);
})
},
Python 方法如下所示:
def update_template_country(request):
pprint(request.POST) # prints an empty QueryDict
id = request.POST.get('id')
country = request.POST.get('country')
print(id, country) #prints None None
return HttpResponse("The country is changed") # this is being returned back to the client
顶部的console.log 消息打印出我的预期,由于没有错误,我假设 CSRF 标头令牌很好。我是否遗漏了一些明显或误解了它的工作原理?
编辑:查看 Chrome 网络选项卡,似乎数据正在正确“发布”:
它显示了这个:
{"id":"593ff2270098e6e8d3292b60","country":"US"}
这正是我所期望的,所以我怀疑问题出在 Django 上。但我看不出那可能是什么。
【问题讨论】:
标签: javascript python django http-post axios