Javacript advanced Programming-ajax and Comet

Source: Internet
Author: User
Tags xml dom document

1. Ajax and Comet1.1 XMLHttpRequest objects

IE5 is the first browser to introduce a XHR object, which is implemented in IE5 through an ActiveX object in the MSXML library. Therefore, in IE may exist msxml2.xmlhttp, msxml2.xmlhttp.3.0, msxml2.xmlhttp.6.0, to support the old browser, you can use the following functions:

function Createxhr () {

if (typeof XMLHttpRequest! = ' undefined ') {

return new XMLHttpRequest ();

}else if (typeof activexobject! = ' undefined ') {

if (typeof arguments.callee.activeXString! = "string") {

var versions = [' MSXML2. xmlhttp.6.0 ', ' MSXML2. xmlhttp.3.0 ', ' MSXML2. XMLHttp '],i,len;

For (i =0, len=versions.length;i<len;i++) {

try{

New ActiveXObject (Versions[i]);

arguments.callee.activeXString = Versions[i];

Break

}cath (ex) {

}

}

}

return new ActiveXObject (arguments.callee.activeXString);

}else{

throw new Error (' NO XHR object. ');

}

}

1.2 XHR Usage

Using the Xhr object, the first method is open (), the first parameter represents the method, the second parameter represents the request address, and the third parameter indicates whether the data is passed asynchronously.

The second step calls the Send () method,

The third part determines the corresponding method according to the return state.

(1). When the server responds, the response data is automatically populated with the properties of the Xhr object.

L ResponseText: Data returned as a response body

L Responsexml: The response content is the XML DOM document data, the content type is "Text/xml" or "Application/xml"

L Status: The state of the response

L StatusText: Status description of the response

(2). For asynchronous requests, you can detect the ReadyState property of the Xhr object.

L 0: not initialized

L 1: Start

L 2: Send

L 3: Receive

L 4: Complete

var xhr = createxhr ();

Xhr.onreadystatechange = function () {//must be specified before the Open method

if (xhr.readystate = = 4) {

if ((Xhr.status >=200 && xhr.status <) | | xhr.status = = 304) {

alert (Xhr.responsetext);

}else{

Alert (' Error ');

}

}

}

Xhr.open (' Get ', ' ex.php ', true);

Xhr.send (NULL);

1.3 GET Request

var xhr = createxhr ();

Xhr.onreadystatechange = function () {//must be specified before the Open method

if (xhr.readystate = = 4) {

if ((Xhr.status >=200 && xhr.status <) | | xhr.status = = 304) {

alert (Xhr.responsetext);

}else{

Alert (' Error ');

}

}

}

Xhr.open (' Get ', ' ex.php?uid=xx ', true); Pass parameters after URL ground

Xhr.send (NULL);

1.4 POST Request

var xhr = createxhr ();

Xhr.onreadystatechange = function () {//must be specified before the Open method

if (xhr.readystate = = 4) {

if ((Xhr.status >= && xhr.status <) | | xhr.status = = 304) {

alert (Xhr.responsetext);

} else {

Alert (' Error ');

}

}

}

Xhr.open (' Post ', ' ex.php ', true);

Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); Specify transport type before sending

Xhr.send (form.data);//The parameter is transmitted in send

1.5 Cross-resource requests

IE8 supports cors through Xdomainrequest objects, other browsers natively support cors through XHR objects, and image Ping and Jsonp are two other cross-browser communication technologies.

Javacript advanced Programming-ajax and Comet

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.