ajax architecture
1.Intitial XMLHttpRequest object and send the Request;
2. assign the process function for the request;
3.send out the http request;
4.process the return result from server.
Ajax example:
<script language="javascript" runat=server>
var http_request_check = false;
function send_request_check(url) {//初始化、指定處理函數、發送請求的函數
http_request_check = false;
//開始初始化XMLHttpRequest對象
if(window.XMLHttpRequest) { //Mozilla 瀏覽器
http_request_check = new XMLHttpRequest();
if (http_request_check.overrideMimeType) {//設定MiME類別
http_request_check.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE瀏覽器
try {
http_request_check = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e)
{
try {
http_request_check = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request_check) { // 異常,建立對象執行個體失敗
window.alert("不能建立XMLHttpRequest對象執行個體.");
return false;
}
http_request_check.onreadystatechange = processRequest_check;
// 確定發送請求的方式和URL以及是否同步執行下段代碼
http_request_check.open("GET", url, true);
http_request_check.send(null);
}
// 處理返回資訊的函數
function processRequest_check() {
if (http_request_check.readyState == 4) { // 判斷對象狀態
if (http_request_check.status == 200) { // 資訊已經成功返回,開始處理資訊
// document.getElementById('returnstr').innerText=http_request.responseText;
alert(http_request_check.responseText);
if(http_request_check.responseText=="user exists")
document.form1.submit1.disabled=true;
else
document.form1.submit1.disabled=false;
} else { //頁面不正常
alert("您所請求的頁面有異常。");
}
}
}
function userCheck() {
var f = document.form1;
var username = f.username.value;
if(username=="") {
window.alert("使用者名稱不可為空。");
f.username.focus();
return false;
}
else {
send_request_check("ajax-1-2.aspx?username="+username+"&method=check");
}
}
</script>
//ajax-1-2.aspx codes
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Xml;
public partial class ajax_1_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string method=Request["method"].ToString();
if (method == "check")
Check();
else if (method == "show")
Show();
}
public void Check()
{
string username = Request["username"].ToString();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("user.config"));
XmlNodeList nodelist = doc.SelectSingleNode("users").ChildNodes;
foreach (XmlNode xn in nodelist)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("Login").ToString() == username)
{
Response.Write("user existes");
return;
}
}
Response.Write("you can use the name");
}
public void Show()
{
string obj = Request["obj"];
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("user.config"));
XmlNodeList nodelist = doc.SelectSingleNode("users").ChildNodes;
if (obj.ToString() == "showAdmin")
{
foreach (XmlNode xn in nodelist)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("Rights").ToString() == "Admin")
Response.Write("--" + xe.GetAttribute("Login").ToString() + "<br>");
XmlNodeList nodelist1 = xn.ChildNodes;
foreach (XmlNode xe1 in nodelist1)
Response.Write("----"+xe1.Name+"="+xe1.InnerText.ToString()+" ");
Response.Write("<br>");
}
}
else if (obj.ToString() == "showUser")
{
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data source=(local); initial catalog=ezplanning;user id=sa_autolink;pwd=Q1234%tt";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select username from Autolink_user";
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
Response.Write("--" + rd["username"].ToString() + "<BR>");
conn.Close();
cmd.Dispose();
}
catch (Exception e)
{
Response.Write(e.Message);
}
}
}
}