Rails不接受AJAX调用中的数组?

时间:2011-04-01 12:26:42

标签: ruby-on-rails ajax

我的垃圾:

jQuery 1.3.2
Rails 2.3.5

如果我执行这样一个简单的AJAX调用:

$.ajax({
    type: "POST",
    url: "/admin/emails/" + id + "/distributions",
    dataType: "script",
    data: { value: ['1', '2'] }
});

仅返回2,而不是12

在Firebug中的HTTP POST标头内,它确实说是发送了两个:

authenticity_token  bMmx0pnJ6ePq6ogwSCR1JH55U7wtrMEOy6ME4rNRmCI=
authenticity_token  bMmx0pnJ6ePq6ogwSCR1JH55U7wtrMEOy6ME4rNRmCI=
value   1
value   2
Source
value=1&value=2&authenticity_token=bMmx0pnJ6ePq6ogwSCR1JH55U7wtrMEOy6ME4rNRmCI%3D&authenticity_token=bMmx0pnJ6ePq6ogwSCR1JH55U7wtrMEOy6ME4rNRmCI%3D

但是当它以create方法点击我的调试器时:

{"authenticity_token"=>"bMmx0pnJ6ePq6ogwSCR1JH55U7wtrMEOy6ME4rNRmCI=",
 "action"=>"create",
 "value"=>"2",
 "controller"=>"admin/distributions",
 "email_id"=>"3"}

这里可能会发生什么?

更新

如果我这样做:

$.ajax({
    type: "POST",
    url: "/admin/emails/" + id + "/distributions",
    dataType: "script",
    data: { value: ["1",[data.value]], type: data.type }
});

我可以让所有的拉链通过..

1 个答案:

答案 0 :(得分:0)

您应该将此类数据格式用于此请求

data: { value[0]: '1', value[1]: '2'}

此代码使正确的哈希

var c = ['1', '2'];
var i = 0;
var b = {}; //hash for data:
c.each(function(zz){
  b["value[" + i +"]"] = zz;
  i=i+1;
});

用法:

data: b