0

我有简单的ajax查询

$.ajax(
{
    url:'ADMPANEL_POST.php',
    type:'POST',
    data:{'z':'1'},
    dataType:'json',
    success:function (result) {
        alert( "Data recieved: " + result.x );
        $('#result').append(result.x);
    }
});

在 ADMPANEL_POST.php 中,我尝试获取 z=1 并具有适当的处理程序,并且我希望使用 json_encode 将 data_str 返回到 ajax 成功函数

if(isset($_POST['z'])) 
{
    $init_x = 'test';
    $data_str[] = array('x' => $init_x);
    if($_POST['z']=='1') {
        echo json_encode($data_str, true);
    }
}

但是我在警报消息中收到未定义的 data.x 值。无法弄清楚是什么问题。谢谢。PS:看到了这个,但它没有帮助JSON 返回未定义

4

1 回答 1

3

你的格式是错误的,如果你想result.x == 'test'使用 $data_str = array('x' => $init_x);.

于 2013-03-23T18:41:33.103 回答