Native JS Ajax,get and POST request instance code _javascript tips

Source: Internet
Author: User

The Ajax GET request code for the

Javascript/js is as follows:

<script type= "Text/javascript" > * * Create XMLHttpRequest Object * * var xmlHttp; function Getxmlhttpobject () {if (window). 
  XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest (); 
  }else{//code for IE6, IE5 xmlhttp=new activexobject ("Microsoft.XMLHTTP"); 
return XMLHTTP; 
  //-----------Ajax method-----------//function Getlabelsget () {xmlhttp=getxmlhttpobject (); if (xmlhttp==null) {alert (' Your browser does not support ajax! 
    '); 
  Return 
  var id = document.getElementById (' id '). value; 
  var url= "http://www.Leefrom.com?id=" +id+ "&t/" +math.random (); 
  Xmlhttp.open ("Get", url,true); 
Xmlhttp.onreadystatechange=favorok;//sends the event, receives the information to call the function xmlhttp.send (); function Getokget () {if (xmlhttp.readystate==1| | xmlhttp.readystate==2| | xmlhttp.readystate==3) {//local prompt: Load in} if (xmlhttp.readystate==4 && xmlhttp.status==200) {var d= xm 
    Lhttp.responsetext; Processing return results}} </script>

Javascript/js POST request for Ajax:

 <script type= "Text/javascript" >/* Create XMLHttpRequest Object * * var xmlHttp; function Getxmlhttpobject () {if (window). 
XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest (); 
}else{//code for IE6, IE5 xmlhttp=new activexobject ("Microsoft.XMLHTTP"); 
return XMLHTTP; 
//-----------Ajax method-----------//function Getlabelspost () {xmlhttp=getxmlhttpobject (); if (xmlhttp==null) {alert (' Your browser does not support ajax! 
'); 
Return 
var url= "http://www.lifefrom.com/t/" +math.random (); 
Xmlhttp.open ("POST", url,true); 
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); 
Xmlhttp.send (); xmlhttp.onreadystatechange=getlabelsok;//after sending the event, you receive the message Call function} function Getokpost () {if xmlhttp.readystate==1| | xmlhttp.readystate==2| | xmlhttp.readystate==3) {//local prompt: Load/process} if (xmlhttp.readystate==4 && xmlhttp.status==200) {var d=xmlhttp.res Ponsetext; return value//processing return value}} </script> 

Note: XMLHttpRequest is the basis of Ajax, and when creating XMLHttpRequest objects, you must be in the same ' <script></script> ' tag as the Ajax method you write! Otherwise, the AJAX request will go wrong and the data cannot be returned. JAVASCRIPT/JS Ajax Post/get Request.

The above is a small set to introduce the original JS Ajax,get and POST request instance code related knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.