AJAX ?? ?? ?? ?? ??
AJAX ?? ?? ?? ?? ??:
XMLHttpRequest ??? ??? ???? ???? ? ?????.
??? ??? ???? XMLHttpRequest ??? open() ? send() ???? ???? ???.
Property | Description |
open(???,url,async) | ?? ??, URL ? ?? ?? ??? ?????. ??????. (1).method: ?? ??, GET ?? POST. (2).url: ??? ?? ??? ?????. (3).async:true(???) ?? false(??). |
send(string) | ??? ??? ????. string: POST ???? ????? |
1. get? post? ???:
get ???? ? ??? ?? ???? ??? ??? post? ???? ?? ??? ????.
??? ?? ??? ?? ?? ????? ??? ????.
(1) ??? ??(??? ?? ?? ?????? ????)? ???? ????.
(2) ??? ??? ???? ????(POST?? ??? ??? ????).
(3) ? ? ?? ??? ??? ??? ??? ?? ? POST? GET?? ? ????? ??? ? ????.
2. Get ???:
?? get ??? ??? ?????.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://ipnx.cn/" /> <title>php中文網(wǎng)</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "demo/ajax/net/demo.aspx", true); xmlhttp.send(); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>
? ???? ??? ???? ??? ?? ??? ??? ?????.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ajax { public partial class demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Write(System.DateTime.Now); } } }
?? ?? ??: ? IE ????? ???? ???? ?? ? ????. ?, ??? ?? ??? ? ????? ??? ??? ?? ? ?? ??? ??? ????. Google ?? Firefox?? ?? ??? ??? ????.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://www.softwhy.com/" /> <title>php中文網(wǎng)</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "demo/ajax/net/demo.aspx?rnd=" + Math.random(), true); xmlhttp.send(); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>
?? ??? ?? ?????. URL ?? ??? ??? ???? ? ??? ? URL? ???? ???? ????.
GET? ???? ??? ?? ?? ??, URL? ?? ??? ?? ?? ????. ??? ??? ????.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://ipnx.cn/" /> <title>php中文網(wǎng)</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "demo/ajax/net/demoPara.aspx?webName="+escape("php中文網(wǎng)")+"&age=3", true); xmlhttp.send(); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>
??? ???? ??? ???? ?????. ??? C# ?????.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ajax { public partial class demoPara : System.Web.UI.Page { string webName; int age; protected void Page_Load(object sender, EventArgs e) { webName =Server.UrlDecode(Request.QueryString["webName"]); age = Convert.ToInt32(Request.QueryString["age"]); Response.Write("歡迎來到" + webName + ",本站已經(jīng)成立" + age + "周年。"); } } }
3. POST ??:
Look ?? ?:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://ipnx.cn/" /> <title>php中文網(wǎng)</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("POST", "demo/ajax/net/demo.aspx", true); xmlhttp.send(); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>
? ??? post ???? ???? ??? ?? ??? ??? ?????. get ???? ???? ?? ??? ????.
HTML ??? ?? ???? POST?? ?? ?? setRequestHeader()? ???? HTTP ??? ??? ?? send() ????? ??? ???? ??? ? ????.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://ipnx.cn/" /> <title>php中文網(wǎng)</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("POST", "demo/ajax/net/postParam.aspx", true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("webName=php中文網(wǎng)&age=3"); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>