Nurzhan Nogerbek
Nurzhan Nogerbek

Reputation: 5236

AJAX: form.serialize() and csrf token together?

Can someone say whats wrong with this lane data:... inside saveForm function?

I have list of tasks in my page. Every task has there own form where users can send comments. It means that I have several forms in one page. When I add new task AJAX update list of comments and then I try to send comment by one of the form and it raise error : “CSRF token missing or incorrect”. I have {% csrf_token %} in all my forms.

It seems like I need send CSRF in AJAX. Where I did mistake?

function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

var csrftoken = getCookie('csrftoken');

function csrfSafeMethod(method) {
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}

$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
});

// TASK
$(function () {
    var loadForm = function () {
        var btn = $(this);
        $.ajax({
            url: btn.attr("data-url"),
            type: 'get',
            dataType: 'json',
            beforeSend: function () {
                $("#modal").modal("show");
            },
            success: function (data) {
                $("#modal .modal-content").html(data.html_group_task_form);
            }
        });
    };

    var saveForm = function () {
        var form = $(this);
        $.ajax({
            url: form.attr("action"),
            data: form.serialize().append('csrfmiddlewaretoken', getCookie(csrftoken)),
            type: form.attr("method"),
            dataType: 'json',
            success: function (data) {
                if (data.form_is_valid) {
                    $("#task-list").html(data.html_task);
                    $("#modal").modal("hide");
                }
                else {
                    $("#modal .modal-content").html(data.html_task_form);
                }
            }
        });
        return false;
    };
    // Create task
    $("#task-add-button").click(loadForm);
    $("#modal").on("submit", ".js-task-add-form", saveForm);
    // Update task
    $("#task-list").on("click", "#js-edit-task-button", loadForm);
    $("#modal").on("submit", ".js-task-edit-form", saveForm);
});


//TASK COMMENT ADD
$(".task-comment-form").submit(function(event) {
    event.preventDefault();
    console.log(event.preventDefault());
    var form = $(this);
    $.ajax({
        url: form.attr("action"),
        data: form.serialize().append('csrfmiddlewaretoken', getCookie(csrftoken)),
        type: form.attr("method"),
        dataType: 'json',
        success: function (data) {
            var current_group = form.closest('.custom-list-group');
            if (data.form_is_valid) {
                current_group.find(".task-comments").html(data.html_task_comment);
            }
            else {
                current_group.find(".task-comment-form").html(data.html_task_comment_form);
            }
        },
    });
    form[0].reset();
    return false;
});

CODE ABOUT COMMENT ADD:

views.py:

def task_comment_add(request, project_code, task_code):
    data = dict()
    project = get_object_or_404(Project, pk=project_code)
    task = get_object_or_404(Task, pk=task_code)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.author = request.user
            comment.save()
            task.comments.add(comment)
            data['form_is_valid'] = True
            data['html_task_comment'] = render_to_string('project/task_comment_list.html' {'task': group_task})
        else:
            data['form_is_valid'] = False
    else:
        form = CommentForm()
    context = {'project': project, 'task': task, 'form': form}
    data['html_task_comment_form'] = render_to_string('project/task_comment_form.html', context, request=request)
    return JsonResponse(data)

JS:

// TASK COMMENT ADD
$(".task-comment-form").submit(function(event) {
    event.preventDefault();
    console.log(event.preventDefault());
    var form = $(this);
    $.ajax({
        url: form.attr("action"),
        data: form.serialize(),
        type: form.attr("method"),
        dataType: 'json',
        success: function (data) {
            var current_group = form.closest('.custom-list-group');
            if (data.form_is_valid) {
                current_group.find(".task-comments").html(data.html_task_comment);
            }
            else {
                current_group.find(".task-comment-form").html(data.html_task_comment_form);
            }
        }
    });
    form[0].reset();
    return false;
});

Upvotes: 1

Views: 4129

Answers (3)

Jeremy S.
Jeremy S.

Reputation: 1126

Here's what I did that worked for ajax forms with Django:

$('#ajax_form').submit(function(e){
      e.preventDefault(); 
      var form = this;
      var action = $(form).attr('action'); // grab the action as url
      var form_array = $(form).serializeArray(); // use serializeArray
      form_array.push({ // *expand the array to include a csrf token*
        name: 'csrfmiddlewaretoken',
        value: getCookie('csrftoken') // getCookie function from django docs
      });
      var form_data = $.param(form_array); // set it up for submission
      $.post(action,form_data,function(resp){
        // do stuff after receiving resp
      });
    });

Basically, I used jquery's .serializeArray() instead of serialize.

.serializeArray() gives you an array of objects like this:

[{name:'name1',value:'value1'},{name:'name2',value:'value2'}]

calling $.param(serilaized_array_data) turns it into a string for submitting. So the key is to add the csrf token to the array. I did that in the code above on the line from_array.push({... with the *'s in the comments.

Upvotes: 2

Chase
Chase

Reputation: 3105

.append is for DOM elements. The result of .serialize is a string. Why don't you just put the token in a hidden input in the form with a 'name' attr of 'csrfmiddlewaretoken'? That will serialize it with the rest of your form data.

Upvotes: 2

Nurzhan Nogerbek
Nurzhan Nogerbek

Reputation: 5236

The problem was in my view. I use this and error disappeared:

context = {...}
context.update(csrf(request))

Upvotes: 0

Related Questions