调用PHP文件:返回值为空


AJAX Call to PHP file: return value is empty

有一个简单的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数据在哪里?

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);
    }

你应该在你的user.php中有这样的内容:

die(init());