Reputation: 231
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.
Upvotes: 2
Views: 1699
Reputation: 2277
You can't pass the array directly to url,
Use jQuery.param(yourObject)
.
The param() method creates a serialized representation of an array or an object that can be understandable by various frameworks like php, ruby, django etc.
For again converting it to python
from urllib import parse
value = parse.parse_qs(self.request.POST.get('name'))
Note that using prase you can lost some information, so recheck your data after getting it in python.
Upvotes: 1