使用jQuery.ajax post函数将javascript数组中的数据传递给服务器?

时间:2022-12-05 17:07:29

I was wondering if its possible to pass data stored in a javascript array to the server using jQuery's ajax function..

我想知道是否可以使用jQuery的ajax函数将存储在javascript数组中的数据传递给服务器。

In the jQuery documentation it specifies:

在jQuery文档中,它指定:

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

can "data" be set to an array? How would this work given it seems data is expecting key value pairs? I currently just hard code the values but I want it to be a more dynamic approach..my current code is:

可以将“数据”设置为数组吗?鉴于数据似乎期待关键价值对,它将如何运作?我目前只是硬编码值,但我希望它是一个更动态的方法..我目前的代码是:

jQuery.ajax({
            url: "/createtrips/updateitin",
            type: 'POST',
            data: {place1: 'Sydney', place2: 'London'},
            dataType: 'json',
            });

4 个解决方案

#1


8  

I created an array like this:

我创建了一个这样的数组:

var placesfortrip = {};

then added to it like this:

然后像这样添加到它:

placesfortrip["item"+counter] = inputVal;

(where counter is an incremented counter variable) then assigned this to the data property of the ajax call

(其中counter是递增的计数器变量)然后将其分配给ajax调用的data属性

 jQuery.ajax({
            url: "/createtrips/updateitin",
            type: 'POST',
            data: placesfortrip,
            dataType: 'json',
            });

and if I look at the XHR tab in firebug it appears those values get posted!

如果我看一下firebug中的XHR选项卡,那么这些值就会被发布!

#2


2  

Yes, jQuery.ajax() supports the passing of arrays. It simply serializes the array into a name-value string.

是的,jQuery.ajax()支持传递数组。它只是将数组序列化为名称 - 值字符串。

If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

如果value是一个数组,jQuery会根据传统设置的值使用相同的键序列化多个值(如下所述)。

#3


0  

I believe you would like to look into using JSON to pass those values.

我相信您希望使用JSON来传递这些值。

This is a good place to start

这是一个很好的起点

#4


0  

Check out jQuery serialize: http://api.jquery.com/serialize/

查看jQuery序列化:http://api.jquery.com/serialize/

$('form').submit(function() {
  alert($(this).serialize());
  return false;
});
This produces a standard-looking query string:

a=1&b=2&c=3&d=4&e=5

#1


8  

I created an array like this:

我创建了一个这样的数组:

var placesfortrip = {};

then added to it like this:

然后像这样添加到它:

placesfortrip["item"+counter] = inputVal;

(where counter is an incremented counter variable) then assigned this to the data property of the ajax call

(其中counter是递增的计数器变量)然后将其分配给ajax调用的data属性

 jQuery.ajax({
            url: "/createtrips/updateitin",
            type: 'POST',
            data: placesfortrip,
            dataType: 'json',
            });

and if I look at the XHR tab in firebug it appears those values get posted!

如果我看一下firebug中的XHR选项卡,那么这些值就会被发布!

#2


2  

Yes, jQuery.ajax() supports the passing of arrays. It simply serializes the array into a name-value string.

是的,jQuery.ajax()支持传递数组。它只是将数组序列化为名称 - 值字符串。

If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

如果value是一个数组,jQuery会根据传统设置的值使用相同的键序列化多个值(如下所述)。

#3


0  

I believe you would like to look into using JSON to pass those values.

我相信您希望使用JSON来传递这些值。

This is a good place to start

这是一个很好的起点

#4


0  

Check out jQuery serialize: http://api.jquery.com/serialize/

查看jQuery序列化:http://api.jquery.com/serialize/

$('form').submit(function() {
  alert($(this).serialize());
  return false;
});
This produces a standard-looking query string:

a=1&b=2&c=3&d=4&e=5