AJAX Post & Get Syntax

  • GET method
    xmlhttp.open("GET","demo_get2.asp?fname=Henry&lname=Ford",true);
    xmlhttp.send();
    
  • POST method
    xmlhttp.open("POST","ajax_test.asp",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("fname=Henry&lname=Ford");
    
  • Status
    Property Description
    onreadystatechange Stores a function (or the name of a function) to be called automatically each time the readyState property changes
    readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4:
    0: request not initialized
    1: server connection established
    2: request received
    3: processing request
    4: request finished and response is ready
    status 200: "OK"
    404: Page not found

  • Example
    <script type="text/javascript">
    function loadXMLDoc()
    {
     var xmlhttp;
     if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
        }
     else
      {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     xmlhttp.onreadystatechange=function()
      {
       if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
       }
      }
     xmlhttp.open("GET","ajax_info.txt",true);
     xmlhttp.send();
    }
    </script>