【发布时间】:2015-08-20 10:09:08
【问题描述】:
目前我正在尝试找到一种方法,通过 ajax 从 jQuery 向服务器发送多维数组。这是我的代码:
$.ajax({
type: "POST",
url: "/save",
data: ["string", [["string1", "string2"], ["string 3", "string 4"]]],
// it is arbitrary how many of the ["string", "string"] type arrays
// there could be in the second element of the containing array.
callback: save, // Don't worry about this function, it currently doesn't get called.
dataType: "text"
});
现在发生的事情是,当服务器接收到它并且puts params.inspect 它输出{undefined=>""}。我认为解决办法可能在于将数组转换为jQuery中的字符串,并在服务器收到后将其转换回来,但我不知道。
附:对于那些可以在 vanilla js 但不能在 jQuery 中执行此操作的人,我要做的就是向 /save 发送一个带有 1 个参数的 ajax 发布请求:["string", [["string1", "string2"], ["string 3", "string 4"]]]。
更新
我使用谷歌浏览器检查元素菜单来查看发送到服务器的请求。服务器按预期响应,当我查看发送的表单数据时,我看到了这个(发送时 {"1": ["match", [["a", "a"], ["s", "s" ]]]}):
1[]:match
1[1][0][]:a
1[1][0][]:a
1[1][1][]:s
1[1][1][]:s
这是否表示表单已正确发送?
【问题讨论】:
标签: jquery multidimensional-array sinatra