Leakbali
w3 Tutorial, Web Tutorial
Switch to English Bahasa Indonesia 
Register   Login

AJAX - Request


The XMLHttpRequest object is used to exchange data with a server. To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object:
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
GET is simpler and faster than POST, and can be used in most cases. However, always use POST requests when:
  • A cached file is not an option (update a file or database on the server)
  • Sending a large amount of data to the server (POST has no size limitations)
  • Sending user input (which can contain unknown characters), POST is more robust and secure than GET
A simple GET request:
xmlhttp.open("GET","get_ajax.php",true); xmlhttp.send();

Simple GET Request Example

<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>

Simple GET Request Display

AJAX

Download the Source Code

Download simple GET AJAX : getajax01.zip
In the example above, you may get a cached result. To avoid this, add a unique ID to the URL:
xmlhttp.open("GET","get_ajax.php?t=" + Math.random(),true);
xmlhttp.send();

Unique ID to URL - GET AJAX Example

<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>

Unique ID to URL - GET AJAX Display

Click the button several times to see if the time changes, or if the file is cached.

Download the Source Code

Download GET AJAX with if The File Cached : getajax02.zip
If you want to send information with the GET method, add the information to the URL:
xmlhttp.open("GET","get_ajax.php?fname=Citra&lname=Angkasa",true);
xmlhttp.send();

GET Method Information Example

<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>

GET Methode Information Display

AJAX

Download the Source Code

Download GET AJAX with Information Sent : getajax03.zip
A simple POST request:
xmlhttp.open("POST","post_ajax.php",true);
xmlhttp.send();

POST Request Example

<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>

POST Request Display

Download Post Request Source Code

Download Post Request Source Code : postajax01.zip
To POST data like an HTML form, add an HTTP header with setRequestHeader(). Specify the data you want to send in the send() method:

POST Request Example

xmlhttp.open("POST","post_ajax.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Putra&lname=Angkasa");

Form HTML POST Request Example - POST AJAX

<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>

Form HTML POST Request Display

Download HTML Form Post Request Source Code

Download HTML Form Post Request Source Code : postajax02.zip
Method Description
setRequestHeader(header,value) Adds HTTP headers to the request.

header: specifies the header name
value: specifies the header value
The url parameter of the open() method, is an address to a file on a server:
xmlhttp.open("GET","get_ajax.php",true);
The file can be any kind of file, like .txt and .xml, or server scripting files like .asp and .php (which can perform actions on the server before sending the response back).
AJAX stands for Asynchronous JavaScript and XML, and for the XMLHttpRequest object to behave as AJAX, the async parameter of the open() method has to be set to true:
xmlhttp.open("GET","get_ajax.php",true);
Sending asynchronously requests is a huge improvement for web developers. Many of the tasks performed on the server are very time consuming. Before AJAX, this operation could cause the application to hang or stop.

With AJAX, the JavaScript does not have to wait for the server response, but can instead:
  • execute other scripts while waiting for server response
  • deal with the response when the response ready
When using async=true, specify a function to execute when the response is ready in the onreadystatechange event:

Async=true Example

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();
To use async=false, change the third parameter in the open() method to false:
xmlhttp.open("GET","ajax_info.txt",false);
Using async=false is not recommended, but for a few small requests this can be ok.

Remember that the JavaScript will NOT continue to execute, until the server response is ready. If the server is busy or slow, the application will hang or stop.

Note: When you use async=false, do NOT write an onreadystatechange function - just put the code after the send() statement:

Async=false Example

xmlhttp.open("GET","get_ajax.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

Tutorial


References

About Us

Home
About Us
Contact Us
Sitemap

Tools

Google PageRank
Alexa Rank
Keywords Density

Accounts

Register Account
Login
Valid XHTML 1.0 TransitionalValid CSS!
Web Directory


2006 - 2012 © Leakbali.com - Free Web Tutorial, Free Web Articles, Web Sharing, Source Codes, Web References