jQuery - 在post请求中传递数组 [英] jQuery - passing arrays in post request

查看:603
本文介绍了jQuery - 在post请求中传递数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在jquery $ .post()请求中传递数组。

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">

从上面的示例HTML中,数组应该是这样= ['1e30','1230']

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

发送$ .post()的jQuery:

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>');
    }
}
});

});

jQuery中的数组变量没有问题,我不知道如何发送发布请求。

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

我应该在服务器中获得这两个值 1e30和1230 。但是我在服务器中获取的值为null。

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.

以下是在Python / Django中获取值的服务器代码:

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

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

谢谢!

推荐答案

您必须使用:

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

这篇关于jQuery - 在post请求中传递数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆