Static page schematic code:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>ajax Test </title>
<script type= "Text/javascript" >
function Btnclick () {
var xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP"); Create XMLHTTP object, written for IE browser, different browser methods
if (!xmlhttp) {
Alert ("Create object Failed");
return false;
}
Xmlhttp.open ("POST", "GETDATE1.ASHX?TX" + New Date (), false); Preparing to issue a POST request to the server's GETDATE1.ASHX
XMLHTTP Open is an asynchronous request and does not wait for the data to be returned, so the onreadystatechange event of the XMLHTTP object needs to be monitored
Xmlhttp.onreadystatechange=function () {
if (xmlhttp.readystate = = 4) {//xmlhttp execution state, 1 loading object, 2 loading complete, 3 fetching data, 4 returning data
if (xmlhttp.status==200) {//Return code 200 for success, HTTP response code
document.getElementById ("Text1"). Value=xmlhttp.responsetext;//responetext is the text returned by the server
}
else{
Alert ("The AJAX server returned an error! ");
}
}
}
Xmlhttp.send (); This is just like the server sending the request
}
</script>
<body>
<input id= "Text1" type= "text"/>
<input id= "Button1" type= "button" value= "button" onclick= "Btnclick ()"/>
</body>
General handler schematic code:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Namespace Ajax Test 1
{
<summary>
Summary description of GetDate1
</summary>
public class Getdate1:ihttphandler
{
public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Text/plain";
Context. Response.Write (DateTime.Now.ToString ());
}
public bool IsReusable
{
Get
{
return false;
}
}
}
}
ASP. html page to implement AJAX-a reference for beginners