1

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?

Mulgard
  • 9,877
  • 34
  • 129
  • 232

2 Answers2

1

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);
    }
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • why should i need that? i call my init method asynchronosly using javascript. so why should i call it in my php file? that makes no sense. – Mulgard Jul 18 '15 at 16:03
  • yes. now it is working. can you explain why echo is working and return isnt? – Mulgard Jul 18 '15 at 16:06
  • proper definition is given in this link:- http://stackoverflow.com/questions/10107144/difference-between-php-echo-and-return-in-terms-of-a-jquery-ajax-call – Alive to die - Anant Jul 18 '15 at 16:11
-2

u should have in your user.php somthing like this:

die(init());
M0rtiis
  • 3,676
  • 1
  • 15
  • 22