如何从jquery和ajax获取多维数组的值?

时间:2014-11-06 03:53:22

标签: php jquery arrays ajax

我从ajax.js发送一个多维数组到test1.php。但后来我不知道如何在test1.php中检索多维数组。我找到了很多例子,但它们对我不起作用。

ajax.js

answers = {};
answers[0] = {};
answers[0]['question_id'] = '12';
answers[0]['answer_id'] = '32';

$.ajax({
    type: "POST",
    url: 'test1.php',
    data: {answers: answers},
    //dataType: "json",

    error: function(jqXHR, textStatus, errorThrown){
        alert(errorThrown);
    },
    success: function(data){
        alert('success!');
    }
});

test1.php

$a=$_POST['answers'][0]['answer_id'];
echo "$a"; //display nothing, I wish to display '32' here

感谢任何帮助。谢谢!

更新:我包含了库和Ajax.js确实给了我一个警报'成功'。所以这不是图书馆的问题。再次感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

试试这个 -

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
answers = {};
answers[0] = {};
answers[0]['question_id'] = '12';
answers[0]['answer_id'] = '32';

$.ajax({
    type: "POST",
    url: 'test1.php',
    data: {answers: answers},
    //dataType: "json",

    error: function(jqXHR, textStatus, errorThrown){
        alert(errorThrown);
    },
    success: function(data){
        alert('success!');
    }
});
</script>

test1.php

<?php
$a=$_POST['answers'][0]['answer_id'];
echo "$a hello"; //display nothing, I wish to display '32' here