(1)
JSON format of client request text: received in JSON format, and then parsed by JS (Eval is an object first, and then)
var text = this.responseText;var book = eval('('+ text +')');document.getElementById('btitle').value = book.title;document.getElementById('bintro').value = book.intro;
// Btitle and bintro are text fields in the client webpage.
Server code:
Method 1: Assume that data is returned from the database. First, the data is written as an array and then converted to JSON by using the function.
<? PHP $ book = array ('title' => 'tianlong Babu ', 'intro' => 'Eight hardships of life'); echo json_encode ($ book);?>
Method 2:
/* If JSON is directly returned, can the following sentence be placed?> It can be used outside */
The detailed code is as follows:
<? PHP?> {Title: 'Dragon Babu ', Intro: 'Eight hardships of life '}
(2)
HTML format of client request text: return the HTML code of a large segment in the background, and direct innerhtml to the front page
var text = this.responseText;document.getElementById("news").innerHTML = text;
Server code: HTML text
Code details:
<? PHP/* fetch n pieces of data from the database */foreach (Array ('news 1', 'news 2', 'news 3') as $ V) {echo '<li>', $ V, '</LI>';}?>