Native JS Ajax

Source: Internet
Author: User

Turn from: HTTPS://WWW.CNBLOGS.COM/YUFANN/P/JS-AJAX.HTML1. Creating an Ajax Object

Non-IE6 browser: var obj = new Xmlhttpreuqest ();

IE6 browser: var obj = new Activexbject ("Microsoft.XMLHTTP");

2. Connect to the server

Obj.open (Request method, URL, whether asynchronous)

How to block caching:

Obj.open (' Get ', ' yan.txt?name= ' +new Date (). GetTime (), true);

3. Sending the request

Obj.send ();

4. Receive return value

Request Status Monitoring: onReadyStateChange event: Triggers when there is communication between Ajax and the server

Mainly through the ReadyState attribute to determine whether there is no end, the end is also not successful, the status attribute to judge

1.----ReadyState Properties: Request Status

0 (uninitialized) has not yet called the Send () method

1 (loaded) has called the Send () method and is sending the request

2 (loading complete) the Send () method executes and has received the full response content

3 (interactive) parsing response content

4 (complete) Response content resolution completed, can be called at the client (completion is not necessarily successful, need status to detect success)

2.---Status property: Request result, success (200) or failure (404): obj.status==200

3.---return value responsetext: Text returned from the server: Obj.responsetext (returned as a string)

Get mode

functionGetajax () {varobj; if(ActiveXObject)//Judging whether it's IE6obj =NewActiveXObject ("Microsoft.XMLHTTP"); Elseobj=NewXMLHttpRequest (); //connecting to a serverObj.open (' Get ', ' http://localhost:8080/yan.ashx?name=123 ',true); //Send RequestObj.send (); Obj.onreadystatechange=function(){               if(obj.readystate===4&&obj.status===200)//If the receive is complete and the request succeedsConsole.log (Obj.responsetext);//Output return information           }        }

Post mode

 functionPostajax () {varobj; if(ActiveXObject)//Judging whether it's IE6obj =NewActiveXObject (' Microsoft.XMLHTTP '); Elseobj=NewXMLHttpRequest (); //connecting to a serverObj.open (' Post ', ' http://localhost:8080/yan.ashx?name=123 ',true); //Set Header informationObj.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded '); //send request, pass dataObj.send ({name: ' 123 ')}); Obj.onreadystatechange=function(){                if(obj.readystate===4&&obj.status===200)//If the receive is complete and the request succeedsConsole.log (Obj.responsetext);//Output return information            }       }

Native JS 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.