A brief talk on JS native ajax,get and Post_javascript skills

Source: Internet
Author: User

Ajax GET request for

Javascript/js:

<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's Ajax POST request:

<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 && xmlhtt p.status==200) {var d=xmlhttp.responsetext;//return value//processing return value}} </script> 

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

The above discussion of JS native ajax,get and post is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.