AJAX调用PHP文件:返回值为空 [英] AJAX Call to PHP file: return value is empty

查看:325
本文介绍了AJAX调用PHP文件:返回值为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的ajax调用:

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

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

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

但我的控制台说:

Success
Output:

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

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

推荐答案

user.php 页面上你需要这样做: -

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

这篇关于AJAX调用PHP文件:返回值为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆