Home  >  Article  >  Backend Development  >  javascript - I have watched several tutorials about ajax. Maybe the tutorials are not deep enough. I would like to ask how the php server returns N pieces of information?

javascript - I have watched several tutorials about ajax. Maybe the tutorials are not deep enough. I would like to ask how the php server returns N pieces of information?

WBOY
WBOYOriginal
2016-07-06 13:51:22957browse

The tutorials I watched all echo string... out a string on the server,
and then hand it over to the front-end js for processing.

I would like to ask how you do it, similar to some search pages that return dozens of results at once?

is to return a string, then use to cut it into an array, and then process it,

Or can php directly return an object or array?

Reply content:

The tutorials I watched all echo string... out a string on the server,
and then hand it over to the front-end js for processing.

I would like to ask how you do it, similar to some search pages that return dozens of results at once?

is to return a string, and then use to cut it into an array, and then process it,

Or can php directly return an object or array?

PHP's echo outputs a string, but you can use json_encode to format an array into a string of json to return multiple pieces of information

<code class="php">
$data = [];

$data[] = 'data1';
$data[] = 'data2';
$data[] = 'data3';
$data[] = 'data4';

$str = json_encode($data);
echo $str;
exit;
</code>

What the front end gets is an array object

javascript - I have watched several tutorials about ajax. Maybe the tutorials are not deep enough. I would like to ask how the php server returns N pieces of information?

json can be an array. . For example, isn’t it like your paging

Returns an array. After parsing, the array can contain a lot of json, and then create a corresponding number of html elements according to the length of the array

Return json data

returns data in json or xml format, json is preferred.

Return the json array, and then traverse

Personally, I think this depends on the processing complexity of the client web page (android, ios, etc. are not included in this list). If it is too complicated, just generate HTML return on the server side.
Otherwise, data in json format will be returned, which will be processed by the customer service end json_encode($list);

Of course it is possible to return to xml, but as of now, json is the mainstream, and the phpoperationjson is also simple

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn