Can';t在django视图中通过PUT Ajax请求接收数据值
我正在执行一个PUT Ajax请求,在数据参数中发送一个值,但在django视图中没有收到任何数据 这是我的代码:Can';t在django视图中通过PUT Ajax请求接收数据值,django,ajax,put,Django,Ajax,Put,我正在执行一个PUT Ajax请求,在数据参数中发送一个值,但在django视图中没有收到任何数据 这是我的代码: function wordUpdate(){ $.ajax({ url:'/socialanalyzer/dictionary_update/', headers: { "X-CSRFToken": $.cookie("csrftoken") }, type: 'PUT', data: {
function wordUpdate(){
$.ajax({
url:'/socialanalyzer/dictionary_update/',
headers: { "X-CSRFToken": $.cookie("csrftoken") },
type: 'PUT',
data: {
word_id: temporalWordToEdit
},success: function(data) {
if (data.code==200) {
alertify.success('Word modified successfully');
var delayInMilliseconds = 2000;
setTimeout(function() {
location.reload(true);
}, delayInMilliseconds);
}else{
console.log('Error, status:',data.code);
alertify.error('Error updating the word');
}
}
})
}
views.py
def put(self, request, *args, **kwargs):
try:
if request.method == 'PUT' and request.is_ajax():
import pdb;pdb.set_trace()
我是否正确地传递了这个?这应该是可行的,但我收到的是空数据:
(Pdb) request.GET
<QueryDict: {}>
我收到了值,但没有将其作为数据发送,而是直接在url中发送,如下所示:
url:'/socialanalyzer/dictionary_update/?word_id='+temporalWordToEdit,
(Pdb)request.GET
我不知道这是正确的方法还是我做错了什么。我知道在PUT请求中,您尝试更新实例,但在url中而不是在数据参数中发送id实例是正确的吗
提前谢谢,如果有任何帮助,我们将不胜感激。我找到了自己问题的答案。根据这一点,在PUT Ajax请求中,参数作为主体发送,而不是作为查询字符串发送,因此在django中,我得到的值如下:
request.body
url:'/socialanalyzer/dictionary_update/?word_id='+temporalWordToEdit,
(Pdb) request.GET
<QueryDict: {'word_id': ['1']}>