AJAX调用PHP文件:返回值为空

时间:2015-07-18 15:53:54

标签: javascript php ajax

我有一个简单的ajax调用:

function init() {
    $.ajax({
        url: "./myFolder/user.php",
        data: {
            action: "init"
        },
        type: "post",
        success: function (output) {
            console.log("Success");
            console.log("Output: " + output);
        }
    });
}

调用PHP init方法,只需返回一些json数据:

function init() {
    $arr = array(
        array(
            "region" => "valore",
            "price" => "valore2"
        ),
        array(
            "region" => "valore",
            "price" => "valore2"
        ),
        array(
            "region" => "valore",
            "price" => "valore2"
        )
    );

    return json_encode($arr);
}

但我的控制台说:

Success
Output:

因此输出变量为空。我的json数据在哪里?

2 个答案:

答案 0 :(得分:1)

user.php页面上您需要执行以下操作: -

function init() {
        $arr = array(
            array(
                "region" => "valore",
                "price" => "valore2"
            ),
            array(
                "region" => "valore",
                "price" => "valore2"
            ),
            array(
                "region" => "valore",
                "price" => "valore2"
            )
        );

        echo  json_encode($arr);
    }

答案 1 :(得分:-2)

你应该在你的user.php中有这样的东西:

die(init());