Native Ajax notation

Source: Internet
Author: User

Although many JS frameworks now provide Ajax call methods, we do not even have to understand their specific implementation of the principle can be directly invoked, but I do not think from the most basic level to understand a thing, it is difficult to do the best.

        var xhr=null; if (window.
        XMLHttpRequest) {//non ie kernel XHR = new XMLHttpRequest (); else if (window.
        ActiveXObject) {//IE kernel, where the earlier version of IE is different, the specific query can be XHR = new ActiveXObject ("Microsoft.XMLHTTP");
        else {XHR = null;

            } if (XHR) {Xhr.open ("get", "ajaxserver.action");
                Xhr.onreadystatechange = function () {//ReadyState value Description//0, initialization, XHR object created, open not yet executed

                1, loading, has called the Open method, but has not sent the request//2, loading completed, request has been sent to complete//3, interactive, can receive part of the data
                Status value Description//200: Success//404: No file found, query or URL//500: Server generated internal error if (xhr.readystate = = 4 && xhr.status = 200) {///Here you can handle the returned content//a
                    Returns JSON or XML data format console.log (xhr.responsetext);
           Active release, JS itself will also be recycled         XHR = null;
            }
            };
        Xhr.send (); }


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.