Ajax and Comet-javascript Advanced Programming Chapter 21st reading notes (1)

Source: Internet
Author: User
Tags xml dom document browser cache in domain

The core of Ajax (asynchronous Javascript + XML) technology is XMLHttpRequest对象 that: XHR . Although the name contains XML, it refers only to the technique of retrieving data from the server without having to refresh the page, and its communication is not necessarily XML data, regardless of the data format.

XMLHttpRequest Object

ie7+, Firefox, Opera, Chrome, and Safari. We can directly use xmlhttprequest the constructor to create the Xhr object.

var xhr = new XMLHttpRequest();

Although the method of creating XHR is different from previous versions of IE7 's browsers, the front-end technology has evolved to today, with few business requirements to support the previous version of IE7. So here I skip this situation.

The use of XHR

When using the Xhr object, the first method to invoke is open() that it accepts 3 parameters:

    1. The type of request to send, such as: Get/post

    2. The requested URL

    3. Whether to send the request asynchronously, this parameter is a Boolean value

xhr.open(‘get‘, ‘example.php‘, false)

Note: The call to the open () method does not actually send a request, just a request to start a send!

in execution open () method, you must re-call send () method to actually initiate an AJAX request.

Xhr.open (' Get ', ' example.txt ', false); Xhr.send (null);

The Send () method receives a parameter, which is the data to be sent as the request principal. If you do not need to send data, you must pass in null because this parameter is required for some browsers.

The request in this example is synchronous, and the JavaScript code waits for the server to respond before executing.
When the response is received, the data for the response is automatically populated with the properties of the Xhr object, with the associated properties:

    1. ResponseText: The text to be returned as the response body.

    2. Responsexml: If the content type of the response is "text/xml" or "application/xml", then this property will hold the XML DOM document containing the response data.

    3. Status: HTTP status of the response

    4. Description of the Statustext:http status

responsetext attribute, and for for non-XML data , the value of the Responsexml attribute will be null .

When a response is received, in general, the status is judged to be 200, which is a sign of the success of this request. At this timeresponseTextThe contents of the property are ready, and if the content type is correct,responseXMLAlso be able to access the.
In addition, the status codestatusIf it is 304, then the requested resource is not modified, it can be used directly in the browser cache, of course, such a response is also valid.

if (  (xhr.status >= 200 && xhr.status < 300)  | |  xhr.status == 304 ) {    alert (xhr.responsetext);} Else{    alert (' fail! status: '  + xhr.status);} 

Some browsers incorrectly report a status code of 204. The ActiveX version of XHR in IE will set 204 to 1223, while the native XHR of IE will normalize 204 to 200. Opera will report a status value of 0 when it gets 204.

Original link: http://www.4455q.com/ajax-comet-javascript-chapter21-note1.html


This article is from the "12888169" blog, please be sure to keep this source http://12898169.blog.51cto.com/12888169/1924594

Ajax and Comet-javascript Advanced Programming Chapter 21st reading notes (1)

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.