What is XMLHttpRequest(XHR)
The XMLHttpRequest object is used to exchange data with a server behind the scenes,
Its part of web pages,without reloading whole page.
It has been available ever since Internet Explorer 5.5 was released in July 2000.
XMLHttpRequest (XHR) is an API that can be used by JavaScript, JScript, VBScript, and other web browser scripting languages to transfer and manipulate XML data to and from a webserver using HTTP, establishing an independent connection channel between a webpage's Client-Side and Server-Side.
XHR request is exchange using XHR object.
The intent is to make web pages feel more responsive by exchanging small amounts of data with the server behind the scenes, so that the entire web page does not have to be reloaded each time the user makes a change. This is meant to increase the web page's interactivity, speed, and usability
XHR method :
1- abort(),
2-getAllResponseHeaders(),
3-getResponseHeader( headerName ),
Creating the Object :
var XmlHttp = new XMLHttpRequest();
XMLHttpRequest Properties
onreadystatechange
An event handler for an event that fires at every state change.
readyState
The readyState property defines the current state of the XMLHttpRequest object.
The following table provides a list of the possible values for the readyState property:
0-The request is not initialized.
1-The request has been set up.
2-The request has been sent.
3-The request is in process.
4-The request is completed.
readyState = 0 After you have created the XMLHttpRequest object, but before you have called the open() method.
readyState = 1 After you have called the open() method, but before you have called send().
readyState = 2 After you have called send().
readyState = 3 After the browser has established a communication with the server, but before the server has completed the response.
readyState = 4 After the request has been completed, and the response data has been completely received from the server.
responseText
Returns the response as a string.
responseXML
Returns the response as XML. This property returns an XML document object, which can be examined and parsed using the W3C DOM node tree methods and properties.
status
Returns the status as a number (e.g., 404 for "Not Found" and 200 for "OK").
statusText
Returns the status as a string (e.g., "Not Found" or "OK").
AJAX Disadvantages
1-Open Source: View source is allowed and anyone can view the code source written for AJAX,
2-It can increase design and development time,
3-Security is less in AJAX application as all files are downloaded at client side.
Javascript Ajax Call:
Using javascript call ajax its very old method generally all people can’t use, because its very old method but i am explain that how its work.
Generally two method are use in ajax :
1-GET,
2-POST,
1-GET :
A "GET" request refers to sending information to the server using parameters tacked on to the current page's URL This can be done by directly adding
2-POST :
Using Ajax to send "POST" requests is very similar to that for "GET" requests, with a couple of differences. Traditionally "POST" is used when the information you're sending exceeds a certain size
Following is simple example of javascript ajax call :
Demo Link :
http://freeteachnology.hol.es/ajax_call/javascript/
Code Link :
1-javascriptHome.php,
2-javascriptSuccess.html,
1-javascriptHome.php :-
Simple GET method using call ajax using XMLHttpRequest object,
Following are simple exmaple of ajax:
Code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript Ajax call</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<br>
<div class="container">
<div class="alert alert-info">
<strong>Hi</strong> The XMLHttpRequest Object.
</div>
<div class="row">
<div class="col-sm-6 col-md-6">
<a id="ajaxButton" class="btn icon-btn btn-info" href="#"> Click here</a>
</div>
</div>
</div>
<br>
<!--Start:: if success ajax call then show message -->
<div id="demo"></div>
<!--End:: if success ajax call then show message -->
<script type="text/javascript">
(function() {
var httpRequest;
document.getElementById("ajaxButton").onclick = function()
{
makeRequest('javascriptSuccess.html');
};
function makeRequest(url) {
httpRequest = new XMLHttpRequest();
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function() {
if (httpRequest.status === 200) {
document.getElementById("demo").innerHTML =httpRequest.responseText;
}
}
//ajax post type:-
httpRequest.open('GET', url);
httpRequest.send();
}
})();
</script>
</body>
</html>
Above example that on click call function makeRequest() and pass object url value, in this function call XMLHttpRequest() object if request is created then show alert cannot create XML instance.
If xml request is create onreadystatechange function call , if http request status 200 then ajax successfully call and success response show in demo id div and http request is GET.
2-javascriptSuccess.html
In javascriptSuccess file contain only static message no more need explaination.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript Ajax call</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="alert-group">
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Well done!</strong>The XMLHttpRequest Object : You successfully read this important alert message.
</div>
</div>
</div>
</div>
</body>
</html>
Comments