开发者

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开发者_如何学C.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); }
    });


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


After a reserach the only solution that worked for me was:

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

Also, use 'choices' with brackets, so you will be able to retrive in the server in a single variable, otherwise it will be an post to each array element. That worked for me. You can see an other post here at stackoverflow.

I hope this will be able to help someone in the future.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜