简体   繁体   中英

return request method = GET but i send request = POST (ajax)

I'm Trying to delete object using ajax with Django. I send request method 'POST' bet return 'GET' and raise error

function delete_bank_question(pk){
    if (confirm('are you sure you want to remove this question?')==true){
        url = "/question/delete/"
        console.log("Deleted clicked");
        $.ajax({
            url : url, 
            type : "POST",
            method : "post",
            data : {'pk': pk}, 
            success : function(json) {
            $('#list_question_pk_'+pk).hide();
            console.log("element deletion successful");
            $("#deleteModal").toggle();
        },
        error : function(xhr,errmsg,err) {
            console.log(xhr.status + ": " + xhr.responseText); 
            console.log("error when removing element");
        }});
    }
    else {
        return false;
    }
};

and there is the view

class QuestionDeleteView(DeleteView):
model = Question
template_name = 'questionbank.html'

def get_object(self, queryset=None):
    pk = self.request.POST.get("pk")
    element = Question.objects.get(pk=pk)
    return element

def get_success_url(self):
    return reverse('questions:questionbank')

the only difference is drawing the HTML by js with ajax (request GET)

function item_builder(data,pk){
    var item = ['<li class="questions-item text-center"  id="list_question_pk_' +pk+ '">'];
    item.push('<div class="col-md-4 "><a href="#" class="question-name pull-right">');
    item.push(data.description);
    item.push('</a></div>');
    item.push('<div class="col-md-4">');
    item.push('<p class="question-type '+css_mapper[data.question_type]+'">');
    item.push(type_mapper[data.question_type]);
    item.push('</p>');
    item.push('</div>');
    item.push('<div class="col-md-4">');
    item.push('<p class="pull-left">');
    item.push('<i class="fa fa-ellipsis-h"></i>');
    item.push('<div class="pull-left question-setting" >');
    item.push('<a href="#" id="edit-icon" class="wow fadeInDown"');
    item.push('data-wow-duration="0.5s">');
    item.push('<i class="fa fa-pencil fa-1x"></i>');
    item.push('</a>')
    item.push('<a href="#" data-toggle="modal" data-pk="'+pk+'" id="delete-icon" ');
    item.push('data-target="#deleteModal" class="wow fadeInDown delete-icon"');
    item.push('data-wow-duration="0.3s">');
    item.push('<i class="fa fa-trash-o fa-1x"></i>');
    item.push('</a>')
    item.push('</div>');
    item.push('</p>');
    item.push('</div>');
    item.push('</li>');
    return item.join(' ');
}

and there is the result from inspect element

PK: 1
list_questions.js:105 Deleted clicked
jquery-1.11.2.min.js:4 GET http://127.0.0.1:8000/en/question/delete/
500 (INTERNAL SERVER ERROR)m.ajaxTransport.a.send @ jquery-   
1.11.2.min.js:4m.extend.ajax @ jquery-
1.11.2.min.js:4delete_bank_question @ list_questions.js:106(anonymous
function) @ list_questions.js:40m.event.dispatch @ jquery-
1.11.2.min.js:3m.event.add.r.handle @ jquery-1.11.2.min.js:3
list_questions.js:118 500: DoesNotExist at /en/question/delete/
Question matching query does not exist.

Request Method: GET
Request URL: http://127.0.0.1:8000/en/question/delete/

anyone can help me ?

Your inspect element result shows that you are sending request by GET method:

...
jquery-1.11.2.min.js:4 GET http://127.0.0.1:8000/en/question/delete/
...

Check your JS code first.

i solved this problem. the request is GET because i forget the CSRF TOKEN in the form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM