JavaScript Learning Ajax

Source: Internet
Author: User

Creation of 1.xmlHttpRequest objects

Ajax core of the XMLHttpRequest object, the following code gives a compatible approach to each browser implementation

function createxhr () {if(typeofXMLHttpRequest! ="undefined"){        return NewXMLHttpRequest (); }Else if(typeofActiveXObject! ='undefined'){        if(typeofArguments.callee.activeXString! ="string"){            varversions=["msxml2.xmlhttp.6.0","msxml2.xmlhttp.3.0","msxml2.xmlhttp"]; varI,len;  for(i=0; len=versions.length;i++){                Try{                    NewActiveXObject (Versions[i]); Arguments.callee.activeXString=Versions[i];  Break; }Catch(ex) {}}} return NewActiveXObject (arguments.callee.activeXString); }Else{        Throw NewError ("no XHR object available ..."); }}

In fact, if you disregard the previous version of IE7, the use of native CHR can be achieved.

2,XHR Common methods

1) Xhr.open (): Accepts three parameters, request type (get,post), requested URL, whether it is an asynchronous request (is true, no is false)

2) Xhr.send (): Get is null,post for the data to be sent.

3) Xhr.setrequestheader (), set the request header

4) Xhr.onreadystatechage set the callback function of the asynchronous request, in order to ensure the compatibility of the browser, you need to specify the callback before open ()

The ReadyState property of the Xhr object changes during the request

1) 0: not initialized, the open () method has not been successfully invoked

2) 1: Start, already called open (), send not yet called ()

3) 2: Send, already called send ()

4) 3: Received, partial response has been received

5) 4: Complete, all response data has been received, the client can use

After the response is received, the response data is automatically populated with the following properties of the XHR object:

1) ResponseText: Response text

2) Responsexml If the response type is "Text/xml" or "Application/xml", the property has a value, otherwise it is empty

3) Status Response State code, general 200 is the flag of success, 304 is the data obtained from the cache

4) statustext response status Description, best not to use, in cross-browser not too reliable

JavaScript Learning Ajax

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.