gpt4 book ai didi

jquery - 通过ajax将数组传递给php?

转载 作者:行者123 更新时间:2023-12-01 03:03:17 25 4
gpt4 key购买 nike

我有这个数组

var car = new Array();
car['brand'] = "Ford";
car['color'] = "red";
car['type'] = 1;

现在,我尝试发送此信息,但 php 只告诉我汽车是一个 undefined index :

$.post('regCar.php', { "car[]": car }, function(data) {

// some other lol here ...

});

发送 "car[]": ["Ford", "red", 1] 工作正常:(我如何将关联数组传递给 php?

最佳答案

正如我在评论中提到的,你 should not use arrays this way 。仅将数组用于数字索引:

Associative Arrays are not allowed... or more precisely you are not allowed to use non number indexes for arrays. If you need a map/hash use Object instead of Array in these cases because the features that you want are actually features of Object and not of Array. Array just happens to extend Object (like any other object in JS and therefore you might as well have used Date, RegExp or String).

这正是代码不起作用的原因。

Have a look at this fiddle :警报为空,这意味着 jQuery 根本没有序列化(因此发送)任何数据。

人们可能期望它至少会发送一个空值,例如 car[]= 但显然它不会。

如果您使用对象:

var car = {};

it works

关于jquery - 通过ajax将数组传递给php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5962383/

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