What is the difference between
echo and return in returning ajax results?
$.ajax({ ... success: function (data) { addType = "change"; var obj = JSON.parse(data); console.log(data); } })
$addID = $this->input->post('addId'); $data = $this->Address_model->getAddressListInfo($addID); (1)echo json_encode($data); (2)return json_encode($data);
You can use echo above, but not return! Solution
echo: used to output results
return: return processing results
There is no output result, how does js receive it?
return is the return value in function, which is convenient for subsequent calls
sure Can not be done.
return is the execution result set of the returned method, and there is no output
echo is to output this json
ajax json object You need to get the string
of the parsing output. It is very simple. Suppose you are a json parsing machine yourself. After your code runs and then returns, nothing will be displayed on the browser
. Only by outputting the result can your eyes see it and you guess it can be parsed
return is not returned to js...
Think about it, PHP The return value of the language function is returned to javascript...
This reminds me of a newbie I brought in before who wanted to throw PHP exceptions to the browser...
return
is to return results to the PHP program. echo
is the response of Ajax.
echo means outputting the result to the requester, and return means returning the value to the caller without outputting the result. Ajax obtains data based on page values.
The above is a detailed code example of the difference between echo and return in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!