0

i know that there was similar questions, but i would like to get some clarification here.

With following Ajax setup:

        $.ajaxSetup({
        cache: true,
        dataType: 'json',
        error: function(xhr, status, error){
           console.log(status);
        },
        timeout: 60000, //Timeout of 60s
        type: 'POST',
        url: 'test.php'
    }); //Close $.ajaxSetup()

    $('#openTest').bind('click', function(){
    $.ajax({
        data: {val: "Hello", val2: "Hello2"},
        success: function(response){
            console.log('complete');
            console.log(response);
        }
    });

When 'test.php' is:

<?php
$return= array ('one'=>'one1', 'two'=>'two1');
return json_encode($return);
?>

I am getting parseerror. But when I replace 'return' with an 'echo', it works fine.

<?php
$return= array ('one'=>'one1', 'two'=>'two1');
echo json_encode($return);
?>

I will be retrieving much more complex data via this $.ajax calls, and I was expecting 'return' to works, 'echo' doesn't seems to me like good solution.

So, what are you suggesting? Is there something wrong with the Ajax setup, or call, so 'return' doesn't work, and is 'echo' a good solution?

Thanks.

2
  • 1
    You need to echo it out at some point. Return doesn't output anything. Commented Jan 26, 2013 at 10:27
  • To which element you want to return you json code? You simply want to print/echo your code to a "file" which is requested via the AJAX REQUEST. Echo is correct. Commented Jan 26, 2013 at 10:29

5 Answers 5

1

return returns data to the caller of the function and since you are not in a function you cannot use return. echo prints the data. So echo is the way to go.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, it is much clearer now. I was expecting php script to work as a function.
Returns can also be used in files outside of functions or methods. In that case, the file passes the returned value to any includes made in other files, as well as stopping execution.
1

when you do a return in php, it is not printed. When you do an echo, it gets printed.

Nothing is wrong with php or Ajax, it is just the context which is wrong.

Sinply put, use return when u need to catch the returned data and maybe process it. Best case for using return is in functions.

Use echo when you need to print something directly.

Here in this case using an echo and exit is what i recommend.

Comments

1

Return is used in functions to get the data back and use it in some fashion in your PHP. Echo is used for ajax calls because your PHP code will output the data to whatever is calling it (the browser, your ajax call, etc).

You also probably want to have header('Content-Type: application/json'); in your PHP file to make things all right and proper.

Comments

1

No, there is no problem with your ajax setup, it's because the return is used with PHP objects or variables and it can't return a value to other language like javascript. when you are using echo it sends the values to HTTP response so your ajax response can handle it.

The best way to do complex data is to send them in arrays like:

$arr = array();
$arr['res'] = 'something';
$arr['res2'] = 'somethingelse'; 
echo json_encode($arr);

and then you can handle it as object.parameter in your jquery code as I have specified it before in this example.

Comments

0

Always make echo or print or parsing php as html file in ajax call and then get the data, this will save you a lot of efforts.

the ajax is just reading the file, there is no way to make calls in two different languages PHP and JS. The JS just returned the filed from the server, if it was php then it will interpreted and go to the Ajax call as html or else possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.