AJAX调用只发送最后一个项目,而不是整个数组数组、而不是、项目、AJAX

2023-09-11 01:22:52 作者:Ninety丶所谓爱情。

这是我的AJAX调用:

This is my AJAX call :

$(".all_of_union").live("click", function(){
      id = window.location.href.split("/")[5]

      var data = {};
    data.value = $(".union input").map(function(){
        return $(this).val();
    }).get();
    data.type = 'zip';

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

如果我是这样做警报(data.value)在我的控制台,然后将它正确地显示所有的ZIP codeS。但随着我的断点在我的Rails应用程序精心挑选的,当它击中控制器,只有最后的zip code居然出现了。

If I were to do alert(data.value) in my console, then it reveals all the zip codes correctly. But with my breakpoint carefully selected in my Rails application, when it hits the controller, only the last zip code actually shows up.

就算我送AJAX调用就像这样:

Even if I send the AJAX call like so :

$.ajax({
      type: "POST",
      url: "/admin/emails/" + id + "/distributions",
      dataType: "script",
      data: { value: ["07005", "07034", "07035", "07045", "07046", "07054", "07058", "07082", "07405", "07435", "07438", "07440", "07444", "07457", "07460", "07801", "07803", "07806", "07828", "07830", "07834", "07836", "07840", "07847", "07849", "07850", "07852", "07853", "07856", "07857", "07866", "07869", "07876", "07885", "07920", "07927", "07928", "07930", "07931", "07932", "07933", "07934", "07935", "07936", "07940", "07945", "07946", "07950", "07960", "07976", "07980", "07981", "07999", "07802", "07842", "07845", "07870", "07878", "07926", "07961", "07962", "07963", "07970"], type: data.type }
  });

结果:

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

不过只有最后的zip code表示。

Still only the last zip code shows.

或者我可以哑下来这么简单:

Or I can dumb it down as simple as this :

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

只有 2 返回。

我不能合理地理解这一点。别人?

I can't reasonably understand this. Anyone else?

推荐答案

您可以试试这个

traditional: true

编辑:

我想你的榜样,得到它的工作。 下面是脚本:

I tried your example and got it to work. Here is the script:

  $.ajax({
    type: "POST",
    url: "/home/test/" + 4,
    dataType: "script",
    traditional: true,
    data: { value: ['1', '2'] }
  });

这里是法

[HttpPost]
public ActionResult Test(string id, string[] value)
{
  return RedirectToAction("Index");
}

我得到了阵价值两个值

I get two values in array "value"