Ajax Basics Tutorial (3)-3.1 Processing server response

Source: Internet
Author: User

The XMLHttpRequest object provides two properties that can be used to access the server response. First attribute respo-

Nsetext provides the response as a string, and the second property Responsexml provides the response as an XML object. Some simple use cases are good for getting a response in simple text, such as displaying a response in a warning box, or responding to a word that only indicates success or failure.

The example in chapter 2nd uses the ResponseText property to access the server response and displays the response in the warning box.

3.1.1 Use the innerHTML property to create dynamic content

If the server response is accessed as simple text, the flexibility is poor. Simple text has no structure, it is difficult to use JavaScript for logical representation, and it is difficult to dynamically generate page content.

If you use the innerHTML property of an HTML element together, the ResponseText property becomes useful. The innerHTML attribute is a nonstandard attribute that was first implemented in IE and later used by many other popular browsers. This is a simple string that represents the content between a set of start and end tags.

By using ResponseText and inner-in combination

HTML, the server can "produce" or generate HTML content, and the browser uses the innerHTML property to "consume" or process it. The following example shows a search feature that is implemented using the XMLHttpRequest object, its ResponseText property, and the innerHTML property of the HTML element. Click the Search button to start "Search" on the server and the server will generate a result table in response. The innerHTML property of the DIV element is set to the response-of the XMLHttpRequest object when the browser processes the response

The value of the Text property. Figure 3-1 shows the browser window after clicking the Search button and adding a result table to the contents of the window.

The example in the 2nd chapter simply shows the server response in the warning box, which is similar to the code for this example. The specific steps are as follows:

1. Click the Search button to call the Startrequest function, which first calls the Createxmlhttprequest function to initialize a new instance of the XMLHttpRequest object;

2. The Startrequest function sets the callback function to the Handlestatechange function;

3. The Startrequest function uses the open () method to set the request method (get) and the request target, and is set to complete the request asynchronously;

4. Send the request using the Send () method of the XMLHttpRequest object;

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.