Developer Snippet Diary

AJAX REQUEST Using javascript

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest(); //object for requests
  xhttp.onreadystatechange = function() { //called when the readyState property changes
    if (this.readyState == 4 && this.status == 200) { //means responce ready && status is OK
      document.getElementById("demo").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "ajax_info9.txt", true);
  xhttp.send(); 
  xhttp.open("GET", "ajax_info.txt", true);
  xhttp.send();
}
</script>
  • open(method, url[, async, user, psw]); //execute other scripts while waiting for server response, deal with response when ready if async is true
  • send(); //FOR GET
  • send("fname=Henry&lname=Ford"); //send request for post

readyState Holds the status of the XMLHttpRequest.

0: request not initialized

1: server connection established

2: request received

3: processing request

4: request finished and response is ready

 

STATUS:Returns the status-number of a request

200: "OK"

403: "Forbidden"

404: "Not Found" 

Posted by: R GONDAL
Email: rizikmw@gmail.com