zoukankan      html  css  js  c++  java
  • AJAX Asynchronous JavaScript and XML

    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);
                }
               
               
            }
        }
    }
     

     


  • 相关阅读:
    LeetCode_1.Two Sum
    F#周报2018年第48期
    使用Kdenlive为视频加入马赛克特效
    网络安全从入门到精通(第一章)前言篇
    hdu 5023 线段树染色问题
    poj 2528 线段树离散化+染色
    字符Hash初步
    经典二分:秦腾与教学评估
    国王游戏
    Trie:hdu 4825、1251、1247、Poj 3764
  • 原文地址:https://www.cnblogs.com/Winston/p/1026146.html
Copyright © 2011-2022 走看看