JQuery after array through ajax

I have an array (for checkboxes) that I need to pass alongside the regular form in the ajax message but cannot make this work:

new_data = [a,b,c,d,e];

somedata_assoc = JQuery.param({'choices[]': new_data});

    $.ajax({
        type: "POST",
     url: contract_qurl,
     data: $(div).find("form").serialize()+"&"+somedata_assoc,
     context: $(this),
     success: function(data) { $("#results_table").html(data); }
    });
+5
source share
2 answers

I get javascript error in this line

new_data = [a,b,c,d,e];

I had to change it to

new_data = ['a','b','c','d','e']; 

you are j capitalized in jQuery on this line

somedata_assoc = JQuery.param({'choices[]': new_data});

should be this (or just a $ transcript)

somedata_assoc = jQuery.param({'choices': new_data});

too, I don’t think you need parentheses, in most cases they will make it harder to retrieve data on the server

+7
source

After restarting, the only solution that worked for me was the following:

url='url/to/page'
choices = [1,2,3,4,5,6]
    $.post(url,{ 'choices[]': choices }, function(data){
          console.log(data);
    },'html');

, "" , , . . fooobar.com/questions/154257/....

, - .

+7

All Articles