rnk
rnk

Reputation: 2204

jQuery - passing arrays in post request

I want to pass arrays in the jquery $.post() request.

HTML:

<input type="checkbox" id="add-check-1e30">
<input type="checkbox" id="add-check-1230">

From the above example HTML the array should be like this = ['1e30', '1230']

jQuery for sending $.post():

$("#addbox-add").click(function(){

var ukeys = new Array();
$("input[type=checkbox]:checked").each( function() {
a = $(this).attr("id");
b = a.split('-').pop();
ukeys.push(b);
});

$(".addbox").remove();

$.post("/information/portfolio/add/", {'ukeys': ukeys }, function(data) {
for(i=0; i<data.length; i++)
{
    ukey = data[i].ukey;
    image = data[i].image;
    service = data[i].service;
    if(data[i].smallimage != "")
    {
        image = data[i].smallimage;
    }
    if (image == null)
    {
        $(".portfolio-preview").append('<li class="portfolio-item" id="portfolio-item-'+service+'-'+ukey+'"></li>');
    }
    else
    {
        $(".portfolio-preview").append('<li class="portfolio-item" id="portfolio-item-'+service+'-'+ukey+'"><img class="portfolio-image" src="'+image+'" width="150px" height="150px"></li>');
    }
}
});

});

There is no problem with the array variable in jQuery, I don't know how to send it in the post request.

I'm supposed to get those two values 1e30 and 1230 in the server. But the values I'm getting in the server is null.

Here is the server code for fetching the values in Python/Django:

def portfolio_add(request):
    ukeys = request.POST.get('ukeys', '')   
    .....etc.....etc......

Thanks!

Upvotes: 0

Views: 4160

Answers (1)

Goin
Goin

Reputation: 3964

You have to use:

request.POST.getlist('ukeys', '')

Upvotes: 3

Related Questions