gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:04:16 27 4
gpt4 key购买 nike

这是我的 AJAX 调用:

$(".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
});
});

如果我在我的控制台中执行 alert(data.value),那么它会正确显示所有邮政编码。但是,在我的 Rails 应用程序中仔细选择断点后,当它到达 Controller 时,实际上只显示最后一个邮政编码。

即使我像这样发送 AJAX 调用:

$.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"}

仍然只显示最后一个邮政编码。

或者我可以像这样简单地简化它:

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

只有 2 会返回。

我无法合理地理解这一点。还有人吗?

最佳答案

你可以试试这个

traditional: true

编辑:

我试过你的例子并让它工作。这是脚本:

  $.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");
}

我在数组“值”中得到两个值

关于javascript - AJAX 调用只发送最后一项,而不是整个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5512744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com