I'm doing a PUT Ajax request sending a value in data parameter, but i don't receive any data in my django view.
This is my code:
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()
Am I passing this in properly? This should be works, but I receive empty data:
(Pdb) request.GET
<QueryDict: {}>
I also tried sending data with JSON.stringify function, but i get the same result:
var word_id = { 'word_id': temporalWordToEdit };
$.ajax({
url:'/socialanalyzer/dictionary_update/',
headers: { "X-CSRFToken": $.cookie("csrftoken") },
type: 'PUT',
data: JSON.stringify(word_id)
I received the value, but instead of sending it as data, i sent it directly in the url like this:
url:'/socialanalyzer/dictionary_update/?word_id='+temporalWordToEdit,
(Pdb) request.GET
<QueryDict: {'word_id': ['1']}>
I don't know if this is the right way or I'm doing something wrong. I understand that in a PUT request, you try to update an instance, but it's correct to send the id instance in the url instead of in the data parameter?
Thanks in advance, any help will be apprecciate