Switch to full style
Codes,problems ,discussions and solutions
Post a reply

AJAX - Sending a request to a server

Mon Aug 17, 2009 1:21 pm

AJAX - Sending a request to a server
To send off a request to the server, we use the open() and send() methods.

The open() method takes three arguments. The first argument defines which method to use when sending the request (GET or POST). The second argument specifies the URL of the server-side script. The third argument specifies that the request should be handled asynchronously.

The send() method sends the request off to the server. If we assume that the HTML and ASP file are in the same directory, the code would be:
javascript code
xmlhttp.open("GET","time.asp",true);
xmlhttp.send(null);


Now we must decide when the AJAX function should be executed.

We will let the function run "behind the scenes" when a user types something in the "Name" field:
html code
<form name="myForm">
Name: <input type="text" name="username" onkeyup="ajaxFunction();" />
Time: <input type="text" name="time" />
</form>

Our updated "testAjax.htm" file now looks like this:
html code
<html>
<body>

<script type="text/javascript">
function ajaxFunction()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
document.myForm.time.value=xmlhttp.responseText;
}
}
xmlhttp.open("GET","time.asp",true);
xmlhttp.send(null);
}
</script>

<form name="myForm">
Name: <input type="text" name="username" onkeyup="ajaxFunction();" />
Time: <input type="text" name="time" />
</form>

</body>
</html>




Post a reply
  Related Posts  to : AJAX - Sending a request to a server
 Sending a post request using AJAX     -  
 Sending One lakh character in ajax     -  
 Ajax Source code to Suggest application with JSP Server side     -  
 Sql server or windows server question?     -  
 sending sms from bluetooth mobile to pc     -  
 Help Sending e-mail from VB.Net application     -  
 sending parameters into XSLT sheet     -  
 Sending and playing microphone audio over network     -  
 What is AJAX, How to start AJAX?     -  
 sending sms using java code and bluetoth device pls send the     -  

Topic Tags

AJAX Request