0

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?

3
  • Possibly because you're sending interestedIn as an array. How are you deserialising that on the server? Alternatively it may be that the array is out of scope of your AJAX request. It's hard to say with the small snippet of code shown Commented Apr 24, 2018 at 15:17
  • No deserialisation server side - I tried client side and it through a few $ errors. Commented Apr 24, 2018 at 15:18
  • I don't know Django but, when you say None, you mean the variable/array is empty? also, can you verify on server side if request.POST.get('interestedIn') is indeed of type array? Commented Apr 24, 2018 at 15:18

1 Answer 1

2

You can try :

data: { 'email': values['email'], 'name': values['name'], 'interestedIn': JSON.stringify(interestedIn)}
Sign up to request clarification or add additional context in comments.

1 Comment

This worked - but transports the array as a string to server side so needs a little more manipulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.