hyun k
hyun k

Reputation: 191

Calling jquery ajax gives me a 405(Method Not Allowed:) error

I have a question

Calling jquery ajax gives me a 405(Method Not Allowed:) error The error message doesn't tell me why. The request itself seems to be the cause of the problem. if you know what's the problem for this problem thanks for let me know~!

ajax

$('body').on('click', '.skill_search_button', function (e) {
    e.preventDefault();
    window.history.pushState("", "", '/wm/myshortcut/')
    const search_word = $(".skill_input_box").val();
    console.log("search_word : " + search_word);

    $("input:radio.search").each(function () {
        if (jQuery(this).is(":checked")) {
            search_option = this.id;
        } else {
            // alert("check")
        }
    });
    $.ajax({
        type: "POST",
        url: 'search_by_id_and_word/',
        data: {
            'search_word': search_word,
            'search_option': search_option,
            'page_user': "{{page_user}}",
            csrfmiddlewaretoken: '{{ csrf_token }}'
            },
        success: function (result) {
            window.history.pushState("", "", '/wm/myshortcut/')
            $("#wm_list_area_for_popup").html("")
            $("#wm_list_area_for_popup").append(result)
        }
    });
});

url

    path('myshortcut/search_by_id_and_word/' , views.searchSkilNoteViewByIdAndWord.as_view(), name="search_by_id_and_word"),

view

class searchSkilNoteViewByIdAndWord(ListView):
    model = MyShortCut
    paginate_by = 10
    template_name = 'wm/MyShortCut_list_for_search.html'

    def get_queryset(self):
        if request.method == "POST" and request.is_ajax():
            search_user_id = request.user
            search_word = request.POST['search_word']
            search_option = request.POST['search_option']
            print("search_user_id : ", search_user_id)
            print("search_word : ", search_word)
            print("search_option : ", search_option)
            user = User.objects.get(username=search_user_id)
            qs = MyShortCut.objects.filter(Q(author = user)).filter(Q(title__icontains=search_word) | Q(content1__icontains=search_word) | Q(content2__icontains=search_word)).order_by('-category')
            return qs
        else:
            qs = MyShortCut.objects.filter(Q(author = user)).filter(Q(title__icontains=search_word) | Q(content1__icontains=search_word) | Q(content2__icontains=search_word)).order_by('-category')
            return qs

Upvotes: 2

Views: 202

Answers (2)

Mehdi Payervand
Mehdi Payervand

Reputation: 251

At server side you should care about what model would be acceptable,in this case i think models are different or method VERB in both side should be same:

client      server
 POST        POST

Upvotes: 1

Lewis
Lewis

Reputation: 2798

You need to change your template tag:

from

csrfmiddlewaretoken: '{{ csrf_token }}'

to

csrfmiddlewaretoken : $("input[name=csrfmiddlewaretoken]").val()

Also include {% csrf_token %} in your template

More information on Cross Site Request Forgery in [Django-Docs]

Upvotes: 1

Related Questions