简体   繁体   中英

AJAX Call to PHP file: return value is empty

I have a simple ajax call:

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

The PHP init method gets called and simply should return some json data:

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

    return json_encode($arr);
}

but my console says:

Success
Output:

So the output variable is empty. Where is my json data?

On user.php page you need to do :-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM