GCien
GCien

Reputation: 2349

ajax POST for data which contains an array with jQuery and Django

So I'm looking to take data for checkboxes which have been checked, and I'm pushing these values to an array:

var interestedIn = [];

$(":checked").each(function() {
  interestedIn.push($(this).val());
});

I'm then passing this array as data to be POSTED by ajax call with some other variables:

$.ajax({
    type: "POST",
    dataType: "json",
    url: "{{ request.path }}",
    data: { 'email': values['email'], 'name': values['name'], 'interestedIn': interestedIn }
})

I'm trying to then access the interestedIn variable with a Django views.py file:

if request.POST and request.is_ajax():
    interestedin = request.POST.get('interestedIn')

When printing the interestedin variable to inspect, this is coming back as None. Does anyone know how you post arrays and variables simultaneously?

Upvotes: 0

Views: 46

Answers (1)

Freyre
Freyre

Reputation: 36

You can try :

data: { 'email': values['email'], 'name': values['name'], 'interestedIn': JSON.stringify(interestedIn)}

Upvotes: 2

Related Questions