使用jQuery.ajax post函数将javascript数组中的数据传递给服务器?
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选项卡,那么这些值就会被发布!
更多相关文章
- 如何更新jqgrid中的数据值
- 创建一个未排序的数组,其中包含重复元素和唯一元素的总和
- js不使用jquery,调用ajax,传递数组,并接受,
- 键盘功能无法处理数据表搜索
- 如何将表列转换为数组?
- 点击JSON数据加载Galleria画廊。我需要新鲜的眼睛来看我的错误
- 如何修复JSON对象的假数组?
- 如何使用ajax GET或POST方法将数据传递到amazon lambda节点。js
- 如何从SQL SELECT查询中的c#变量创建jQuery数组