如何在$ .params中动态传递参数数组

时间:2017-09-24 08:41:30

标签: javascript angularjs

我正在尝试以x-www-formurlencoded格式发送此输入参数:

customer_id,
xyz,
order_total,
order_location,
item_id[0]
item_quantity[0],
res_id,
item_id[1],
item_quantity[1]

现在我的网络请求代码如下所示:

return $http({
                method: "POST",
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Authorization': 'Bearer ' + user_token
                },
                url: config.server_url + "orders",
                data: $.param({
                    'customer_id': user_id,
                    'xyz': '',
                    'order_total': '50.09',
                    'order_location': 'abc',
                    'res_id': res_id,
                    'item_id[0]':'dkkdskds',
                    'item_quanitity[0]':1,
                    'item_id[1]':'dkkdskds',
                    'item_quanitity[1]':1,
                    'item_id[2]':'dkkdskds',
                    'item_quanitity[2]':1,
                })
            });

现在,我不知道如何动态传递item_id [0],item_id [1],item_id [2],...

1 个答案:

答案 0 :(得分:1)

尝试以下伪代码:

var array1 = ['dkkdskds' , 'dkkdskds2', 'dkkdskds3'] ;
var array2 = [1 , 2, 3] ;

return $http({
   method: "POST",
   headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Authorization': 'Bearer ' + user_token
         },
           url: config.server_url + "orders",
           data: $.param({
                    'customer_id': user_id,
                    'xyz': '',
                    'order_total': '50.09',
                    'order_location': 'abc',
                    'res_id': res_id,
                    'item_id': array1,
                    'item_quanitity': array2
                })
            });