简体   繁体   中英

Merge serialized form with array

I have a form and I am sending it with ajax to server. Like this:

$.ajax({
   url: form.attr('action'),
   type: 'POST',
   data: form.serialize(),
   dataType : 'json',
   success: function(data){
   }
});

And I receive in server something like

Array
(
    [forms_element_1] => 'some value 1',
    [forms_element_2] => 'some value 2',
    [forms_element_3] => 'some value 3'
)

Now i need to add to this form global variable that is array itself.

var statuses = [5,7,3];

I need to receive from POST in server side something like

Array
(
    [forms_element_1] => 'some value 1',
    [forms_element_2] => 'some value 2',
    [forms_element_3] => 'some value 3',
    [statuses] => Array
    (
         [0] => 5,
         [1] => 7,
         [2] => 3
    )
)

How can I achieve that in jQuery?

使用$.param将其转换为参数字符串,然后将其附加到序列化形式(这也是参数字符串)中。

data: form.serialize() + "&" + $.param({statuses:[5, 7, 3]}),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM