jQuery $.getJSON : 将数组传递给 PHP

jQuery $.getJSON : Passing arrays to PHP

本文关键字:PHP 数组 getJSON jQuery      更新时间:2023-09-26

我有javascript将数组传递给PHP:

         var mapIDArray = ["4f8d7684791635ec2e000000", "4f8cbc087916359181000000"];
         $.getJSON("rebound.php",
            { 
                'mapIDs[]' : mapIDArray
            },
            function(output){
                console.log(output);
            }
        );

在 rebound.php 中,我尝试读取传递的数组(var_dump、print_r 等),例如:

print_r($_GET['mapIDs[]']);

但是没有运气...

不需要将[]添加到名称中。

var mapIDArray = ["4f8d7684791635ec2e000000", "4f8cbc087916359181000000"];
$.getJSON("rebound.php",
{ 
   mapIDs: mapIDArray
},
function(output){
    console.log(output);
});

然后在你的 PHP 中:$_GET['mapIDs']将是一个数组。

它会

在服务器端print_r($_GET['mapIDs']);,并且

var mapIDArray = ["4f8d7684791635ec2e000000", "4f8cbc087916359181000000"];
$.ajax("rebound.php",
            dataType: 'json',
            data:
            { 
                'mapIDs[]' : mapIDArray[0],
                'mapIds[]' : mapIdArray[1],
            },
            traditional: true,
            success: function(output){
                console.log(output);
            }
        );

在客户端。关键问题是,首先,PHP 将其视为一个名为"mapID"的数组,即使它是 mapIDs[] 作为 GET 参数,其次,多个字段需要多个条目。