jQuery Jquery通过ajax发布数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2687831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery post array via ajax
提问by Dan
I have an array (for checkboxes) that I need to pass alongside the regular form in an ajax post, but can't seem to get this to work:
我有一个数组(用于复选框),我需要在 ajax 帖子中与常规表单一起传递,但似乎无法使其正常工作:
new_data = [a,b,c,d,e];
somedata_assoc = JQuery.param({'choices[]': new_data});
$.ajax({
type: "POST",
url: contract_qurl,
data: $(div).find("form").serialize()+"&"+somedata_assoc,
context: $(this),
success: function(data) { $("#results_table").html(data); }
});
回答by Yisroel
I'm getting a javascript error on this line
我在这一行收到一个 javascript 错误
new_data = [a,b,c,d,e];
I had to change it to this
我不得不把它改成这个
new_data = ['a','b','c','d','e'];
you capitalized the J in jQuery in this line
您在这一行中将 jQuery 中的 J 大写
somedata_assoc = JQuery.param({'choices[]': new_data});
should be this (or just the $ shorthand)
应该是这个(或只是 $ 简写)
somedata_assoc = jQuery.param({'choices': new_data});
also, i dont think you need the brackets, in most cases they would make it more difficult to retrieve the data on the server
另外,我认为您不需要括号,在大多数情况下,它们会使检索服务器上的数据变得更加困难
回答by Jonathas Pacífico
After a reserach the only solution that worked for me was:
经过研究,唯一对我有用的解决方案是:
url='url/to/page'
choices = [1,2,3,4,5,6]
$.post(url,{ 'choices[]': choices }, function(data){
console.log(data);
},'html');
Also, use 'choices' with brackets, so you will be able to retrive in the server in a single variable, otherwise it will be an post to each array element. That worked for me. You can see an other post here at stackoverflow.
此外,使用带括号的“选择”,这样您就可以在服务器中以单个变量检索,否则它将成为每个数组元素的帖子。那对我有用。你可以在 stackoverflow 上看到另一篇文章。
I hope this will be able to help someone in the future.
我希望这能在将来对某人有所帮助。