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:
The type of request to send, such as: Get/post
The requested URL
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:
ResponseText: The text to be returned as the response body.
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.
Status: HTTP status of the response
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 timeresponseText
The contents of the property are ready, and if the content type is correct,responseXML
Also be able to access the.
In addition, the status codestatus
If 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)