Ajax

Ajax

From ThaiiS Note (Wiki)

Jump to: navigation, search

Contents

Create an XMLHttpRequest Object

xmlhttp=new XMLHttpRequest();

ie5-6

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

Send a Request To a Server

(Ex.)
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();

Example

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
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>
</head>

Server Response

  • responseText get the response data as a string
  • responseXML get the response data as XML data

The responseText Property (Ex.)

document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

The responseXML Property (Ex.)

xmlDoc=xmlhttp.responseXML;
var txt="";
x=xmlDoc.getElementsByTagName("ARTIST");
for (i=0;i<x.length;i++)
  {
  txt=txt + x[i].childNodes[0].nodeValue + "<br />";
  }
document.getElementById("myDiv").innerHTML=txt;


Retrieved from "http://www.thaiis.eu/Ajax"