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;