I am trying to send via AJAX an array (or object) to my Django View.
In JS File:
var dataArr = [];
dataArr.push({'status':'active'});
...
var json = JSON.stringify(dataArr);
...
$.ajax({
type: "POST",
url: "/filter/",
data: {
'filter_text' : json,
'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
},
success: filterSuccess,
dataType: 'json',
contentType: 'application/json'
});
I am getting a 403 Forbidden error in Javascript. I tried several things, e.g. omitting the dataType / contentType, sending a Javascript object instead of an array, etc.
@csrf_exemptdecorator to your view and if it will work - the problem is in$("input[name=csrfmiddlewaretoken]").val(). Are you sure the token is being sent at all? Try to useconsole.log($("input[name=csrfmiddlewaretoken]").val())before ajax method.'csrfmiddlewaretoken': '{{ csrf_token }}'$.ajax({ type: "POST", url: "/filter/", traditional: true, data: { 'status_filter' : statusArr, 'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val() }, success: filterSuccess, });