AJAX Asynchronous JavaScript and XML

來源:互聯網
上載者:User
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()+"&nbsp");
                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);
            }
           
           
        }
    }
}
 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.