使用Ajax动态加载数据

一、xmlHttpHelper.js代码

function XmlHttpHelper(){}

XmlHttpHelper.__getXmlHttpObj = function()

{

 try

 {

  return new ActiveXObject("MSXML2.XMLHTTP");

 }

 catch(e)

 {

  try

  {

   return new XMLHttpRequest();

  }

  catch(ee)

  {

   throw(new Error(-1, "无法创建XMLHTTP对象。"));

  }

 }

};

//

//  使用XMLHTTP和远程服务器通信。

//

// async   是否为异步方式:true/false

// httpMethod  http方法:"post"/"get"

// responseType 返回数据的类型:"text"/"xml"/null

// url    请求的URL地址

// callback  异步操作完成时执行的回调函数

// postData  post方式时发送的数据

//

XmlHttpHelper.transmit = function(async, httpMethod, responseType, url, callback, postData)

{

    var xmlhttp = this.__getXmlHttpObj();

 xmlhttp.open(httpMethod, url, async);

 

 if(!async && httpMethod.toLowerCase() == "post")

 {

  xmlhttp.setRequestHeader('Content-Length', postData.length);

  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

 }

 if(async)

 {

  xmlhttp.onreadystatechange = function()

  {

   if(xmlhttp.readyState == 4)

   {

       try

       {

           if(responseType != null)

           {

            if(responseType.toLowerCase() == "text")

             callback(xmlhttp.responseText);

            else if(responseType.toLowerCase() == "xml")

             callback(xmlhttp.responseXML);

           }

           else

           {

         callback(null);

        }

    }

    finally

    {

        xmlhttp = null;

    }

   }

  }

  xmlhttp.send(postData);

 }

 else

 {

  xmlhttp.send(postData);

  if(xmlhttp.status == 200)

  {

      if(responseType != null)

      {

       if(responseType.toLowerCase() == "text")

        return xmlhttp.responseText;

       else if(responseType.toLowerCase() == "xml")

        return xmlhttp.responseXML;

      }

   else

   {

    return null;

      }

  }

  return null;

 }

}; 

二、Process.ashx

<%@ WebHandler Language="C#" Class="Process" %>

using System;

using System.Web;

using System.Data;

using System.Xml;

using System.Data.SqlClient;

public class Process : IHttpHandler

{   

    public void ProcessRequest (HttpContext context)

    {

            context.Response.Cache.SetCacheability(HttpCacheability.Private);

            string StrID = context.Request.QueryString["ID"];

            string ReturnValue=string.Empty;

            if (StrID == null)

            {

                return;

            }

            if (StrID.Equals("ID1"))

            {

                string Str1 = "<table border=1><tr><td>计算机与信息科学系</td></tr></table>";

                Str1 = Str1.Replace("<", "&lt;").Replace(">", "&gt;");

  

                ReturnValue = "<I><R>1</R><V>" +Str1+ "</V></I>";

            }

            else

            {

                ReturnValue = "<I><R>1</R><V>西南林学院</V></I>";

            }

       

            context.Response.ContentType = "text/XML";

            context.Response.Write(ReturnValue);

            context.Response.End();

   }

    public bool IsReusable

    {

        get {

            return false;

        }

    }

}

三、Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W 3C //DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>无标题页</title>

</head>

<script type="text/javascript" src="xmlHttpHelper.js"></script>

<script type="text/javascript">

    function ShowDiv(ObjID)

    {

        var MyImgObj=document.getElementsByName(ObjID);

        var responseData = XmlHttpHelper.transmit(false, "get", "xml", "Process.ashx?ID=" + encodeURI(ObjID), null, null);

        if(responseData.selectSingleNode("/I/R").text == "1")

        {

          var ClXx=responseData.selectSingleNode("/I/V").text;

          DivID.innerHTML=ClXx;

          DivID.style.left=422;

          DivID.style.top=40;

          //DivID.style.display="";     

        }   

    }

</script>

<body>

    <form id="form1" runat="server">

    <div>

        <img id="ID1" οnmοuseοver='ShowDiv("ID1")' alt="Test" src="identify.bmp"/></div>

    <div>

        <img id="ID2" alt="Test" οnmοuseοver='ShowDiv("ID2")' src="identify.bmp"/><br />

        <br />

        <br />

        &nbsp;</div><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <div id="DivID">

        </div>

    </form>

   

</body>

</html>

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值