Saturday, 02 February 2013 13:55

how to implement ajax in web page

Written by 
Rate this item
(0 votes)

Today ajax is being a common part in most large website and
application.user loves the way everything done without page
refresh and website's with ajax implemented is getting higher
popularity.in the following i showed how to use ajax in asp.net
web page.you can use in php too -

in head section -

<script type="text/javascript" language="javascript">
var xmlHttpRequest;

function GetCurTime() {
//create XMLHttpRequest object
xmlHttpRequest = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");

//browser doesn’t support Ajax,so exit
if (xmlHttpRequest == null)
return;

//Init XMLHttpRequest object
xmlHttpRequest.open("POST", "http://localhost/Jquery/Ajax/TimeAjaxTest.aspx", true);

//callback function setting
xmlHttpRequest.onreadystatechange = StateChange;

xmlHttpRequest.send(null);
}
function StateChange() {

if (xmlHttpRequest.readyState == 4) {
alert(xmlHttpRequest.responseText);
document.getElementById('lblTime').value = xmlHttpRequest.responseText;

}
}
</script>

in page -

<a onclick="GetCurTime();">Ajax test by normal javascript</a> <input id="lblTime"/>

here i posted to TimeAjaxTest.aspx page.in that page's code behind file put this
in Page Load method -

protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second);
}

Read 3302 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.