Native JavaScript completes AJAX requests

Source: Internet
Author: User
Tags response code

To complete the AJAX request using native JavaScript, you should first create an object XMLHttpRequest, taking into account the low version of IE browser, using the ActiveXObject object, the code into the following:

varrequest;if(Windoe. Xmlhtprequest) {Request=NewXMLHttpRequest ();}Else{Request=NewActiveXObject ();}functionSuccess (text) {vartextarea = document.getElementById (' Test-ie-response-text ')); Textarea.value=text;}functionfail (code) {vartextarea = document.getElementById (' Test-ie-response-text ')); Textarea.value= ' Error code: ' +Code;}varRequest =NewActiveXObject (' microsoft.xmlhttp ');//New Microsoft.XMLHTTP ObjectRequest.onreadystatechange=function() {//function is callback when state changes    if(Request.readystate = = 4) {//successfully completed        //To determine the response result:        if(Request.status = = 200) {            //successful, get the text of the response via ResponseText:            returnsuccess (Request.responsetext); } Else {            //failure, according to the response code to determine the reason for failure:            returnfail (request.status); }    } Else {        //the HTTP request is still continuing ...    }}//Send request:Request.open (' GET ', '/api/categories '); Request.send (); Alert (' Request sent, please wait for response ... ');

Native JavaScript completes AJAX requests

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.