
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
| Method | Description |
|---|---|
| open(method,url,async) | Specifies the type of request, the URL, and if the request should be handled asynchronously or not. method: the type of request: GET or POST url: the location of the file on the server async: true (asynchronous) or false (synchronous) |
| send(string) | Sends the request off to the server. string: Only used for POST requests |
<html>
<head>
<script type="text/javascript">
function simpleAjax()
{
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("Divku").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax_get.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="simpleAjax()">Request data</button>
<div id="Divku"></div>
</body>
</html>
xmlhttp.open("GET","get_ajax.php?t=" + Math.random(),true);
xmlhttp.send();
<html>
<head>
<script type="text/javascript">
function getAJAXid()
{
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("myLayer2").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get_ajax_id.php?t=" + Math.random(),true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="getAJAXid()">Request data</button>
<p>Click the button several times to see if the time changes, or if the file is cached.</p>
<div id="myLayer2"></div>
</body>
</html>
Click the button several times to see if the time changes, or if the file is cached.
xmlhttp.open("GET","get_ajax.php?fname=Citra&lname=Angkasa",true);
xmlhttp.send();
<html>
<head>
<script type="text/javascript">
function getAjax2()
{
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("layerku").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get_ajax2.php?fname=Putra&lname=Angkasa",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="getAjax2()">Request data</button>
<div id="layerku"></div>
</body>
</html>
xmlhttp.open("POST","post_ajax.php",true);
xmlhttp.send();
<html>
<head>
<script type="text/javascript">
function postAjax()
{
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("postLayer").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","post_ajax.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="postAjax()">Request data</button>
<div id="postLayer"></div>
</body>
</html>
xmlhttp.open("POST","post_ajax.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Putra&lname=Angkasa");
<html>
<head>
<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("POST","get_ajax.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>
</body>
</html>
| Method | Description |
|---|---|
| setRequestHeader(header,value) | Adds HTTP headers to the request. header: specifies the header name value: specifies the header value |
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get_ajax.txt",true);
xmlhttp.send();
xmlhttp.open("GET","get_ajax.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;