如何通过jQuery.ajax将JavaScript数组发送到PHP?

时间:2013-11-29 02:25:42

标签: javascript php json jquery

我正在尝试通过jQuery.ajax将JavaScript数组发送到PHP页面,但该数组仅使用空值发送。

如果我在Chrome上打开F12控制台并检查JS对象,它就在那里。全部填满了。但是当我使用ChromePhp工具记录PHP变量时,它只显示空白值(同样,如果我遍历php-array,回显它的值,我会得到所有空白)。

我在这里很困惑。

这是我的示例代码:

<?php 
include 'ChromePhp.php';
if (isset($_GET['newUsers'])) {
    $newUsers = $_GET['newUsers'];
    ChromePhp::log($newUsers);
} else { ?>

<html>
<body>

    <script src="js/jquery-2.0.3.min.js"></script>
    <script type="text/javascript">
        var newUsers = [];

        newUser = [];
        newUser['nome'] = 'alvaro';
        newUser['idade'] = '34';
        newUsers.push(newUser);

        newUser1 = [];
        newUser1['nome'] = 'bia';
        newUser1['idade'] = '7';
        newUsers.push(newUser1);

        newUser2 = [];
        newUser2['nome'] = 'alice';
        newUser2['idade'] = '2';
        newUsers.push(newUser2);

        $.ajax({
            url: "testcookie.php",
            type: "GET",
            data: {
                'newUsers[]': newUsers
            }
        });

    </script>
</body>
</html>
<?php } ?>

已更新根据第一条评论。现在我得传递对象,但不知道如何读取它的属性。已经尝试了$ user ['nome']没有结果。

<?php 
include 'ChromePhp.php';

if (isset($_POST['newUsers'])) {

    $newUsers = $_POST['newUsers'];

    foreach ($newUsers as $user) {
        # code...
        # HOW DO I READ THE nome AND idade VALUES HERE?
    }

} else { ?>

<html>
<body>

    <script src="js/jquery-2.0.3.min.js"></script>
    <script type="text/javascript">
        //var newUsersObj = {};
        var newUsers = [];

        newUser = {};
        newUser['nome'] = 'alvaro';
        newUser['idade'] = '34';
        newUsers.push(newUser);

        newUser1 = {};
        newUser1['nome'] = 'bia';
        newUser1['idade'] = '7';
        newUsers.push(newUser1);

        newUser2 = {};
        newUser2['nome'] = 'alice';
        newUser2['idade'] = '2';
        newUsers.push(newUser2);

        $.ajax({
            url: "testcookie.php",
            type: "POST",
            data: {
                'newUsers[]': newUsers
            },
            success: function () {

            },
            error: function () {

            }
        });

    </script>
</body>
</html>
<?php } ?>

2 个答案:

答案 0 :(得分:4)

只需传递数组或对象,jQuery就会自动将数组或对象转换为正确的参数,如a = [1,2] - &gt;一个[] = 1&安培;一个[] = 2

 data: {
    'newUsers': newUsers
 }

实现这一目标的内部功能是http://api.jquery.com/jQuery.param/

答案 1 :(得分:3)

知道了!

要在PHP中正确访问JavaScrtipt对象,我需要在推送阵列时 JSON.stringify 。然后,在PHP上, json_decode 将它们转换为PHP对象,并使用' - &gt;'访问它们的属性操作

最终解决方案如下:

<?php 
include 'ChromePhp.php';

if (isset($_POST['newUsers'])) {

    $newUsers = $_POST['newUsers'];

    foreach ($newUsers as $user) {
        # code...
        $usr = json_decode($user);
        ChromePhp::log("Nome: " . $usr->nome . " - Idade: " . $usr->idade);
    }

} else { ?>

<html>
<body>

    <script src="js/jquery-2.0.3.min.js"></script>
    <script type="text/javascript">
        //var newUsersObj = {};
        var newUsers = [];

        newUser = {};
        newUser['nome'] = 'alvaro';
        newUser['idade'] = '34';
        newUsers.push(JSON.stringify(newUser));

        newUser1 = {};
        newUser1['nome'] = 'bia';
        newUser1['idade'] = '7';
        newUsers.push(JSON.stringify(newUser1));

        newUser2 = {};
        newUser2['nome'] = 'alice';
        newUser2['idade'] = '2';
        newUsers.push(JSON.stringify(newUser2));

        $.ajax({
            url: "testcookie.php",
            type: "POST",
            data: {
                'newUsers[]': newUsers
            },
            success: function () {

            },
            error: function () {

            }
        });

    </script>
</body>
</html>
<?php } ?>