jQuery 没有数据,当通过 Ajax POST 传递数组时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4530954/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:24:59  来源:igfitidea点击:

No data, when passing array trough an Ajax POST

jqueryajaxarrayspost

提问by Mars

I'm trying to pass an array trough an Ajax POST request using jQuery. I can't seem to get it done. Here is my code:

我正在尝试使用 jQuery 通过 Ajax POST 请求传递数组。我似乎无法完成它。这是我的代码:

var settings = [];
$('.settingp input').each(function(){
    settings[$(this).attr('id')] = $(this).val();
});
$.post("editSettings.php", { 'settings': settings });

The request happens, but there is no data in it. Any idea what am I doing wrong?

请求发生,但其中没有数据。知道我做错了什么吗?

回答by Mars

the problem was

问题是

$(this).attr('id') => retuns a string, not a number

settings[$(this).attr('id')] = $(this).val();

changed it to and now it works

将其更改为现在可以使用了

settings[settings.length] = [$(this).attr('id'), $(this).val()];

thanks everybody for trying to help me

感谢大家试图帮助我

回答by Travis Derouin

What about this?

那这个呢?

var settings = {};
$('.settingp input').each(function(){
    settings[$(this).attr('id')] = $(this).val();
});
$.post("editSettings.php", settings);

I think the issue is with our settings is initialized. Then when posting it, you don't have to call it "settings", since you have set all of the names of the post values already.

我认为问题在于我们的设置已初始化。然后在发布它时,您不必将其称为“设置”,因为您已经设置了所有发布值的名称。