Ajax request data and status codes in native and jquery packages

Source: Internet
Author: User

Native AJAX Request data

Btn.addeventlistener (' click ',function(){        if(window. XMLHttpRequest) {varXHR =Newwindow.        XMLHttpRequest (); }Else{            //compatible with IE browser            varXHR =NewActiveXObject (' Microsoft.XMLHTTP '); }        //Request Method Get/post        //Request URL        //ture asynchronous request; false synchronous requestXhr.open (' Get ', '/ajax/getdata ',true); //changes to XHR binding event listening state (status code see below)Xhr.onreadystatechange =function(){            if(Xhr.readystate = = 4 && xhr.status = 200) {console.log (xhr.responsetext); }        }        //Send Request Data//get method The send parameter is empty or nullXhr.send (); });

Ajax encapsulation implemented by JQuery

// jQuery    $ ('. Btn '). On (' click ',function() {            $.ajax ({                URL:'/ajax/getdata ',                type:') Get ',                dataType:' json ',                // successfully execute function                success:functions  (response,status) {                   console.log (responsetext);                }            );        });

Xhr.readystate Status Code

0:unsend//Current request not yet sent

1:opened//url Address is open

2:headers_received//Response header information has been received

3:loading//Primary return data is being prepared for processing on the server

The contents of the 4:done//response body have been successfully returned to the client

Xhr.status Server Status Code

200 and starting with 2: success (the body of the response has successfully returned to the client)

301: Permanent Redirect/Transfer

302: Temporary REDIRECT/transfer

304: This time gets the content is read the cache

400: Incorrect parameter for server with client

401: No Access

404: Access address does not exist

500: Unknown server error

501: Server overload

The data is not complete for reference only----------

Ajax request data and status codes in native and jquery packages

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.