您的位置:首页 > 其它

使用Ajax动态加载数据

2007-12-02 11:35 537 查看
一、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("<", "<").Replace(">", ">");

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 "-//W3C//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" onmouseover='ShowDiv("ID1")' alt="Test" src="identify.bmp"/></div>
<div>
<img id="ID2" alt="Test" onmouseover='ShowDiv("ID2")' src="identify.bmp"/><br />
<br />
<br />
 </div><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<div id="DivID">
</div>
</form>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: