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

 

相关推荐:

批量按比例更新 XML 中所有商品价格值

本文介绍如何使用PowerShell脚本高效、准确地批量修改大型电商XML目录中元素的数值——支持按指定百分比(如涨价10%或降价25%)统一调整上千条商品的价格,无需手动编辑,安全可靠。 本文介绍如何使用powershell脚本高效、准确地批量修改大型电商xml目录中``元素的数值——支持按指定百...

使用 Ajax 轮询 Celery 任务状态并实时更新 Django 前端

本文详解如何在django中通过ajax轮询celery异步任务状态,动态获取数据库进度数据并在前端实时渲染,涵盖后端视图设计、任务状态判断、前端递归轮询实现及关键注意事项。 在构建需长时间运行后台任务(如批量数据导入、ETL处理)的Django应用时,用户常需感知任务执行进度而非被动等待。Cele...

如何使用 Ajax 轮询 Celery 任务状态并实时更新 Django 前端

本文详解如何在django中结合celery异步任务与ajax轮询,实现数据库填充过程的实时进度反馈——包括后端状态判断、json响应设计、前端递归轮询及dom动态更新。 在构建需长时间运行后台任务(如批量数据导入、报表生成)的Django应用时,用户不应面对空白页或无响应等待。Celery提供了强...

使用 XSLT 3 和 SaxonC 自动识别并包装 XML 中的软件名称

本文介绍如何利用xslt3的高级文本处理能力(配合python的`saxonche`库),精准定位xml` `标签内嵌套``等子元素时的软件名称,并基于json提供的上下文与偏移信息,将其安全包裹在``标签中,避免破坏原有结构。 在科研文献或技术文档的XML标注任务中,常需从混合内容(mixedco...

python怎么读取xml

Python读取XML主要用xml.etree.ElementTree:ET.parse()解析文件获ElementTree对象再.getroot()得根元素;ET.fromstring()直接解析字符串得根元素;常用root.find()、findall()、.text、.get()等操作提取数据...

PythonDjango进阶开发教程_完整网站架构与功能实现

Django进阶开发核心在于分层清晰的项目结构、健壮的数据建模、安全的用户交互与生产就绪实践。需按功能域拆分应用,封装业务逻辑至services,分离环境配置;模型承载业务规则,优化查询并加密敏感字段;权限分三层控制,表单前后端验证一致;日志分级、缓存防雪崩、Celery异步解耦、静态资源走CDN。...