POST jQuery array to Django

前端 未结 2 586
心在旅途
心在旅途 2021-02-01 20:11

I\'m trying to POST a jQuery array of simple numbers to Django, and I really can\'t make it work. I need a little help on this. I\'m getting an Http 500 with the following error

2条回答
  •  故里飘歌
    2021-02-01 20:26

    You can try to use tasks[] instead of tasks as parameter when sending via ajax. Example:

    $('.btn-group').find('#mark_as_done').on('click', function() {
        var tasks = grab_selected();
    
        $.ajax({
            type: 'POST',
            url: '/edit_lists/',
            data: {'tasks[]': tasks},
        });
    });
    

    Another thing is you are simply returning return tasks in edit_lists() view, you have return a HttpResponse instance or use shortcut like render:

    from django.http import HttpResponse
    
    def edit_lists(request):
        tasks = request.POST.getlist('tasks[]')
        return HttpResponse('Success')
    

    Hope it helps,

提交回复
热议问题