你好,
我有一个 django 应用程序,我想通过单击不在<form>
<button>
来进行 POST
问题是发送到视图函数的数据没有被正确发送。
这是 axios 代码:
axios({
method: 'post',
url: '/url/to/view'
data: {
obj_id: objectid,
content_type: contenttype,
like: !is_liked
},
xsrfCookieName: 'csrftoken',
xsrfHeaderName: 'X-CSRFToken',
headers: {'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
}).then(function (response) {
console.log(response);
});
在这里查看代码:
def my_view(request):
print request.POST
return HttpResponse('XX')
我无法访问 request.POST 数据,因为键是整个字典,值是空列表:
<QueryDict: {u'{"obj_id":"182","content_type":"video","like":true}': [u'']}>
最有用的评论
问题是对象默认序列化为 JSON,而您需要以“application/x-www-form-urlencoded”格式发送数据。 最简单的解决方案是使用像qs这样的库:
有关详细信息,请参阅此评论。
希望这可以帮助!