ASP. Net + HTML page to implement AJAX signals--a reference for beginners

Source: Internet
Author: User
Tags response code

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.