Obviously Ajax is a technique for accessing data using JavaScript scripts.
AJAX enables the Web page to implement asynchronous updates. This is the local updating of the Web page without reloading the entire page.
XMLHttpRequest is the key to AJAX
Browsers now support XMLHttpRequest objects (IE5 and IE6 use ActiveXObject).
Request data back to the readystate has five states 0: Server uninitialized, 1: Server connection established, 2 requests accepted, 3 request processing, 4 request complete.
Every change of state is a good trigger once onreadystatechange event, status has two states: "OK", 404: "Page not Found"
Here's an AJAX foreground implementation code:
Copy Code code as follows:
<title> Untitled Page </title>
<script type= "Text/javascript" >
function GetName () {
var xmlhttp;
if (window. XMLHttpRequest) {
Xmlhttp=new XMLHttpRequest ();
}else{
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
Xmlhttp.onreadystatechange=function () {
if (xmlhttp.readystate==4 && xmlhttp.status==200) {
Alert ("Hello:" +xmlhttp.responsetext);
}
}
Xmlhttp.open ("Post", "Default.aspx?id=gname", true);
Xmlhttp.send ();
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div> <input id= "Button1" type= "button" value= "button" onclick= "GetName ()"/></p>
</div>
</form>
</body>
Background code:
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
if (request["id"]!=null)
{
Response.Write ("John");
Response.End ();
}
}
Execution result: Figure below
Download attachment code next time, let's see how jquery is requesting data asynchronously.