Veidi täiendatud ajaxi näide

Allikas: Lambda
<html>
<head>
<script>
var request;
var url;
var response;
var eresp;

function myupdatefun() {
  //alert(request.readyState)
  if (request.readyState == 4) {   
       //alert("Server is done!"); 
       if (request.status == 200) {
          //alert("Server sent data ok!");       
          response = request.responseText
          //compres=response;
          //alert(response)          
          eresp=eval(response)          
          compres="<table>\n"
          for (i=0; i<eresp.length; i++) {
            //alert(eresp[i])
            compres=compres+"<tr>\n"
            compres=compres+"<td>"+eresp[i]['id']+"</td>"
            compres=compres+"<td>"+eresp[i]['salvestaja']+"</td>"
            compres=compres+"<td>"+eresp[i]['f0']+"</td>"
            compres=compres+"</tr>\n"
          }
          compres=compres+"</table>"          
          //alert(compres);
          document.getElementById('sisu2').innerHTML=compres;
       } else if (request.status == 404)
         alert("Request URL does not exist");
       else
         alert("Error: status code is " + request.status);   
  }       
         
}  

function ajaxcall() {
  request = new XMLHttpRequest();    
  url = "http://dijkstra.cs.ttu.ee/~tammet/cgi-bin/otsi.py?salvestaja=tanel&table="  
  url=url+document.getElementById('table').value;
  request.open("GET", url, true);
  request.onreadystatechange = myupdatefun;
  request.send(null);
}  


</script>
<body>
Tere kah!<form>
sisend: <input type="text" name="table" id="table"></input>
<input type="button" value="otsi!" onclick="ajaxcall()">
</form>

<div id="sisu2"></div>
</body>