XML format and text format of Ajax attributes and functions and returned values (2)

Source: Internet
Author: User
(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>';}?>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.