Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery post array via ajax

I have an array (for checkboxes) that I need to pass alongside the regular form in an ajax post, but can't seem to get this to 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); }
    });
like image 591
Dan Avatar asked Apr 22 '10 02:04

Dan


People also ask

Can we send array through Ajax?

You can simply pass a JavaScript Array variable in the $. ajax as any other variable.

How can we return an array from Ajax call?

You cannot use the values returned from the AJAX request until that request has completed. You'll need to return a Promise[^], which will return the array value once the request has completed.

How are Comma Separated values passed in Ajax?

Create something like: var dataArgs = { checkedValues: checkedValues, action: action }; and then simply pass dataArgs object to data key as a value.


1 Answers

I'm getting a javascript error on this line

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

I had to change it to this

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

you capitalized the J in jQuery in this line

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

should be this (or just the $ shorthand)

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

also, i dont think you need the brackets, in most cases they would make it more difficult to retrieve the data on the server

like image 114
Yisroel Avatar answered Oct 10 '22 18:10

Yisroel