Saturday, February 25, 2017

AJAX Introduction

AJAX Introduction

AJAX IS Asynchronous JavaScript And XML. AJAX is a misleading name. AJAX applications might use XML to transport data, but it is equally common to transport data as plain text or JSON text.

AJAX just uses a combination of:

  • A browser built-in XMLHttpRequest object
  • JavaScript(to (pre)proecess data recved) and HTML DOM (to display updated data)

see AJAX_example for detail.

AJAX example

<!DOCTYPE html>
<html>
<head>

<meta name = "keywords" content = "ajax,example"/>

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "AJAX.htm", true);
  xhttp.send();
}</script>
</head>

<body>

<div id="demo">
  <h2>Let AJAX change this text</h2>
  <button type="button" onclick="loadDoc()">Change Content</button>
</div>

</body>
</html>