下面这个ajax的公共类是我刚开始学ajax的时候从网上找的,对我的帮助很大,为了回报大家,现把做了点修改的公共类以及在实际应用中的使用方式和应该注意的地方.(注意:我是在.net中应用,如果要在其他地方应用,后台代码就要做相应的修改)
ajax的公共类(文件名:ajax.js)
var lastModified = "Sat, 1 Jan 2005 00:00:00 GMT";
var Request = new function(){
this.pool = new Array();
this.getXMLHttp = function (chunnel)
{
if(chunnel != null)
{
for (var a = 0; a < this.pool.length; a++)
{
if(this.pool[a]["chunnel"] == chunnel)
{
if(this.pool[a]["obj"].readyState == 0 || this.pool[a]["obj"].readyState == 4)
{
return this.pool[a]["obj"];
}
else
{
return "busy";
}
}
}
this.pool[this.pool.length] = new Array();
this.pool[this.pool.length - 1]["obj"] = this.createXMLHttp();
this.pool[this.pool.length - 1]["chunnel"] = chunnel;
return this.pool[this.pool.length - 1]["obj"];
}
for (var i = 0; i < this.pool.length; i++)
{
if (this.pool[i]["obj"].readyState == 0 || this.pool[i]["obj"].readyState == 4)
{
this.pool[i]["obj"].abort();
return this.pool[i]["obj"];
}
}
this.pool[this.pool.length] = new Array();
this.pool[this.pool.length - 1]["obj"] = this.createXMLHttp();
this.pool[this.pool.length - 1]["chunnel"] = "";
return this.pool[this.pool.length - 1]["obj"];
}
this.createXMLHttp = function ()
{
if(window.XMLHttpRequest)
{
var xmlObj = new XMLHttpRequest();
}
else
{
var MSXML = ['Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
for(var n = 0; n < MSXML.length; n++)
{
try
{
var xmlObj = new ActiveXObject(MSXML[n]);
break;
}
catch(e)
{
}
}
}
return xmlObj;
}
this.reSend = function (url,data,callback,chunnel)
{
var objXMLHttp = this.getXMLHttp(chunnel)
if(typeof(objXMLHttp) != "object")
{
return ;
}
url += (url.indexOf("?") >= 0) ? "&nowtime=" + new Date().getTime() : "?nowtime=" + new Date().getTime();
if(data == "")
{
objXMLHttp.open('GET' , url, true);
objXMLHttp.setRequestHeader("If-Modified-Since", lastModified);
objXMLHttp.send('');
}
else
{
objXMLHttp.open('POST' , url, true);
objXMLHttp.setRequestHeader("If-Modified-Since", lastModified);
objXMLHttp.setRequestHeader("Content-Length",data.length);
objXMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
objXMLHttp.send(data);
}
if(typeof(callback) == "function" )
{
objXMLHttp.onreadystatechange = function ()
{
if (objXMLHttp.readyState == 4)
{
if(objXMLHttp.status == 200 || objXMLHttp.status == 304)
{
callback(objXMLHttp);
if (objXMLHttp.getResponseHeader("Last-Modified") != "")
lastModified = objXMLHttp.getResponseHeader("Last-Modified");
}
else
{
alert("Error loading page\n"+ objXMLHttp.status +":"+ objXMLHttp.statusText);
}
}
}
}
}
}
应用公共类(getValidate.aspx)
function getValues()
{
var filt = "1=1";
var url = 'Validate.aspx';
url += "?condStr="+ escape(filt) + "&method=getCountOfAllWh";
var data = '';
Request.reSend(url,data,btn_search);
return false
}
function btn_search(obj)
{
if (obj.responseText == "false")
{
try
{
var d=dialogArguments;
d.document.all("hid_filt").value = filtparent;
window.close();
}
catch(e)
{
alert("操作错误,无法返回查询结果!");
}
return false;
}
else
{
document.getElementById("btn_DoSearch").click();
}
}
这里有一点需要注意的地方就是发送消息的时候所带参数的编码问题,不要用encodeURI或encodeURIComponent,因为这两个要么不能解决中文乱码问题,要么不能解决特殊符号(如:%23),主要原因是ajax发送消息并不像平时我们所做的路径跳转,他本身有自己的编码.如果在回复消息后还要继续操作的话最好把要用的数据也编码一下.
后台处理(Validate.aspx):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Logease.CarryOn.Business;
namespace Logease.CarryOn.Web.airIn.bussOperation
{
/// <summary>
/// Validate 的摘要说明。
/// </summary>
public class Validate : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//string method = System.Web.HttpUtility.UrlDecode(this.Page.Request.QueryString["method"].ToString());
//string condStr = System.Web.HttpUtility.UrlDecode(this.Page.Request.QueryString["condStr"].ToString());
string method = this.Page.Request.QueryString["method"].ToString();
string condStr = this.Page.Request.QueryString["condStr"].ToString();
if (condStr == "none")
condStr = this.Page.Request.Params["filt"].ToString().Trim();
this.Page.Response.AppendHeader("Last-Modified",System.DateTime.Now.ToString("r"));
string backStr = "";
switch(method)
{
case "getCountOfAllWh":
backStr = this.getCountOfAllWh(condStr);
this.Page.Response.Write(backStr);
this.Page.Response.End();
break;
case "getCountOfAllWhDet":
backStr = this.getCountOfAllWhDet(condStr);
this.Page.Response.Write(backStr);
this.Page.Response.End();
break;
default:
this.Page.Response.Write("false");
this.Page.Response.End();
break;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#region 验证查询数据
private string getCountOfAllWh(string condStr)
{
Logease.CarryOn.Business.AIManager aiManager = new AIManager();
if (condStr == "")
return "false";
DataTable dt = aiManager.GetAIRI_WH_ALL(condStr);
if (dt.Rows.Count > 1)
return "true";
return "false";
}
#endregion
#region验证确认数据
private string getCountOfAllWhDet(string condStr)
{
Logease.CarryOn.Business.AIManager aiManager = new AIManager();
if (condStr == "")
return "none";
DataTable dt = aiManager.GetAIRI_WH_DET(condStr);
if (dt.Rows.Count > 0)
return "true";
return "false";
}
#endregion
}
}