Professional Documents
Culture Documents
BaiGiang AJAX
BaiGiang AJAX
1. Gii thiu
1.1 Tng quan v m hnh ng dng Web
- Gim ti cho Server - Tc x l nhanh + Hn ch: - Kh pht trin - i hi phn mm h tr - Kh nng tng thch vi trnh duyt khng cao.
* X l h phc v (Server) + u im: - Pht trin n gin - Trin khai v bo tr d dng - Chia s c d liu + Khuyt im - Giao din ngi dng th s - Tnh tng tc km
V d:
Nu s dng trnh duyt Web IE7 tr ln, Firefox, Chrome, Opera, Safari
var xmlhttp = new XMLHTTPRequest();
M t Xc nh s kin (hm) s c gi khi thuc tnh readState ca i tng thay i Cho bit trng thi ca i tng request: 0 = Cha khi to (uninitialized) 1 = ang ti d liu
readyState
D liu c tr v bi server di dng chui text D liu c tr v bi server di dng i tng d liu (XML, ) M trng thi HTTP c tr v bi server Chui trng thi c tr v bi server
Phng thc
abort() getAllResponseHeaders() getresponseHeader(x) open('method','URL','a')
M t Stops the current request Dng gi yu cu n server Tr v tt c cc tiu (headers) di dng chui Tr v gi tr ca tiu (header) x di dng chui specifies the HTTP method (for example, GET or POST), the target URL, and whether the request should be handled asynchronously (If yes, a='TRue'the default; if no, a='false'.) xc nh phng thc ca HTTP (GET hoc POST), URL l ng dn gi yu cu n server (bt ng b) (nu a=true: mc nh)
send(content) setRequestHeader('x','y')
ASP.NET
using System; using System.Data;
5
public partial class BaiGiang_ServerSide : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string sXML = "<?xml version='1.0' encoding='UTF-8'?>"; sXML = sXML + "<Goc "; sXML = sXML + "ThongBao='Xin cho n vi AJAX'"; sXML = sXML + "/>"; Response.ContentType = "text/xml"; Response.Write(sXML); } }
PHP
<?php header('Content-Type: text/xml'); echo "<?xml version='1.0' encoding='UTF-8'?><Goc ThongBao='Xin cho n vi AJAX'/>"; ?>
3. V d
V d 1: Nhp vo tn ngi s dng. Xut thng bo cho
} return null; } </script> </head> <body> Nhp tn: <input id="txt_Ten" type="text" /> <br /> <input id="Button1" type="button" value="Xut Thng Bo" onclick="XuatThongBao()"/> <div id="KetQua"> </div> </body> </html>
* X l h phc v: XuatThongBaoChao.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="XuatThongBaoChao.aspx.cs" Inherits="clsXuatThongBaoChao" %>
XuatThongBaoChao.aspx.cs
using using using using using using using using using using System; System.Data; System.Configuration; System.Collections; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls;
public partial class clsXuatThongBaoChao : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string sTen; string sThongBao; sTen = Request["Ten"]; sThongBao = "Xin cho bn " + sTen; //to chui string sXML sXML = sXML sXML = sXML sXML = sXML XML = "<?xml version='1.0' encoding='UTF-8'?>"; + "<Goc "; + "ThongBao='" + sThongBao + "'"; + "/>";
Chuoi = document.getElementById("txt_a").value; a = parseInt(Chuoi); Chuoi = document.getElementById("txt_b").value; b = parseInt(Chuoi); //gui ket qua ve server var ThamSo = "a=" + a + "&b=" + b; var URL = "./TinhTong2SoNguyen.aspx" + "?" + ThamSo; xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Your browser does not support AJAX!"); return; } xmlhttp.onreadystatechange=stateChanged;
9
xmlhttp.open("GET",URL,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { var xmlDoc=xmlhttp.responseXML.documentElement; S = xmlDoc.getAttribute("Tong"); Chuoi = "Tng: " + S; var newChild = document.createTextNode(Chuoi); var oldChild = document.getElementById("KetQua").childNodes[0]; document.getElementById("KetQua").replaceChild(newChild, oldChild); } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // dng cho IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // dng cho IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } </script> </head> <body> <h1>Tng 2 s nguyn</h1> a: <input id="txt_a" type="text" /> <br /> b: <input id="txt_b" type="text" /> <br /> <input id="Button1" type="button" value="Tnh tng" onclick="TinhTong()"/> <div id="KetQua"> </div> </body> </html>
10
* X l h phc v TinhTong2SoNguyen.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TinhTong2SoNguyen.aspx.cs" Inherits="clsTinhTong2SoNguyen" %>
TinhTong2SoNguyen.aspx.cs
using using using using using using using using using using System; System.Data; System.Configuration; System.Collections; System.Web; System.Web.Security; System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls;
public partial class clsTinhTong2SoNguyen : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int a; int b; int S; string chuoi; chuoi = Request["a"]; a = int.Parse(chuoi); chuoi = Request["b"]; b = int.Parse(chuoi); S = a + b; //to chui string sXML sXML = sXML sXML = sXML sXML = sXML XML = "<?xml version='1.0' encoding='UTF-8'?>"; + "<Goc "; + "Tong='" + S.ToString() + "'"; + "/>";
11
4. Bi tp
Bi tp 1: Nhp vo 2 cnh gc vung a v b ca tam gic vung. Tnh chiu di cnh huyn ca tam gic. Bi tp 2: Nhp vo 2 s nguyn a v b. Tnh tng, hiu, tch, thng 2 s thc a v b Bi tp 3: Nhp vo 2 cnh a,b ca hnh ch nht. Hy tnh chu vi v din tch ca hnh ch nht . Bi tp 4: Nhp vo 2 phn s A, B. Hy tnh tng, hiu, tch, thng ca 2 phn s. Bi tp 5: Nhp vo 3 s nguyn a, b, c. Kim tra a,b,c xem c phi l 3 cnh ca tam gic khng? Nu l tam gic cho bit loi tam gic (thng, vung, cn, u, vung cn, ) Bi tp 6: Xy dng trang web tm kim tn hc sinh (tng t nh Google) vi c s d liu nh sau:
- Khi nhp gi tr vo tm kim th xut hin cc tn hc sinh tng ng v cho php chn tn hc sinh. Sau hin th cc hc sinh tng ng.
12
13