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选项卡,那么这些值就会被发布!

更多相关文章

  1. 如何更新jqgrid中的数据值
  2. 创建一个未排序的数组,其中包含重复元素和唯一元素的总和
  3. js不使用jquery,调用ajax,传递数组,并接受,
  4. 键盘功能无法处理数据表搜索
  5. 如何将表列转换为数组?
  6. 点击JSON数据加载Galleria画廊。我需要新鲜的眼睛来看我的错误
  7. 如何修复JSON对象的假数组?
  8. 如何使用ajax GET或POST方法将数据传递到amazon lambda节点。js
  9. 如何从SQL SELECT查询中的c#变量创建jQuery数组

随机推荐

  1. PHP 的 new static 和 new self
  2. php验证图片不显示
  3. php获取今天是星期几
  4. TCP 的连接建立与关闭状态及数据传输通信
  5. PHP 开发中涉及到emoji表情的几种处理方
  6. PHP 框架中的日志系统
  7. PHP 实现简易的汉字验证码的思路
  8. PHP+Redis发布订阅
  9. php检查数组下标是否存在
  10. php 与 nginx 的两种处理方式