On the internet to find a few posts, but due to the level of limited, tossing a meeting to achieve a small program. After theoretical understanding, coupled with an actual demo, it is easy for beginners to understand Ajax.
I paste the specific code, convenient and I like the first person to learn understanding.
The procedure is as follows:
The first is the foreground code of the request page:
<! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"><meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title> <script type="Text/javascript">function createxhr () {varXHR =NULL; Try { //Firefox, Opera 8.0+, safari,ie7+XHR =NewXMLHttpRequest (); } Catch(e) {//Internet Explorer Try{XHR=NewActiveXObject ("msxml2.xmlhttp"); } Catch(e) {Try{XHR=NewActiveXObject ("Microsoft.XMLHTTP"); } Catch(e) {xhr=NULL; } } } returnXHR; } function Setcontainer (text) {document.getElementById ("Container"). Value =text; } function Testajax () {varXHR =createxhr (); Xhr.onreadystatechange=function () {if(Xhr.readystate = =4&& Xhr.status = = $) {Setcontainer ('Original Ajax:'+xhr.responsetext); //alert (xhr.status);}} xhr.open ('Get','Webform2.aspx?action=gettime',true); Xhr.send (); } </script>"Button1"Type="Button"Value="Test Ajax"onclick="Testajax ()"/> <input id="Container"Type="text"/></body>Then the background data processing of the requested page
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Xml.Linq;namespace_02{ Public Partial classWebForm2:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) { stringAction = request.querystring["Action"]. ToString (); if(request.querystring["Action"]!=NULL) { Switch(action) { Case "GetTime": Response.Write (GetTime ()); Break; Case "getDate": Response.Write (GetDate ()); Break ; } response.end (); } } Private stringGetTime () {returnDateTime.Now.ToShortTimeString (); } Private stringGetDate () {returnDateTime.Now.ToShortDateString (); } }}
Native Javascript+ajax