[Ajax类]
function Ajax(){
this.xmlhttp = null; //XML对象
this.method = "post"; //执行的方法(post/get)
this.url = ""; //异步调用的页面地址
this.responseText = ""; //异步返回的响应字符串
this.responseXML = ""; //异步返回的响应XML
this.failed = false; //创建对象错误标志
this.synch = false; //同步false/异步true
/******************** 事件 ************************/
this.onLoading = function() {}; //正在发送请求
this.onLoaded = function() {}; //已经接收到全部响应内容
this.onInteractive = function() {}; //正在解析响应内容
this.onCompletion = function() {}; //响应内容解析完成
this.onError = function() {}; //异步错误处理事件
this.onFail = function() {}; //创建对象失败处理世界
/******************** 重置事件 **********************/
this.resetFunctions = function() {
this.onLoading = function() {};
this.onLoaded = function() {};
this.onInteractive = function() {};
this.onCompletion = function() {};
this.onError = function() {};
this.onFail = function() {};
};
/******************* 函数 ********************/
//初始化
this.init = function(){
if(window.XMLHttpRequest){
this.xmlhttp = new XMLHttpRequest();
}else if (window.ActiveXObject){
try{this.xmlhttp = new ActiveXObject("Msxml4.XMLHTTP");}
catch(e){
try{this.xmlhttp = new ActiveXObject("Msxml3.XMLHTTP");}
catch(e){
try{this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
catch(e){
try{this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
catch(oc){this.failed=true;}
}
}
}
}
};
//发送请求
//@param data 发送的数据
//@example send("id=1");
this.send=function(data){
var self=this;
if(this.method=="post") {
this.xmlhttp.open(self.method,self.url,self.synch);
}else{
this.xmlhttp.open(self.method,self.url+"?"+encodeURI(data),self.synch);
}
//添加消息响应头
this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
//异步回调函数
this.xmlhttp.onreadystatechange = function(){
//对象未创建
if (self.failed) {
self.onFail();
return;
}
//消息响应标志
switch (self.xmlhttp.readyState) {
//(未初始化)还没有调用send()方法
case 0:{
break;
}
//(载入)已调用send()方法,正在发送请求
case 1:{
self.onLoading();
break;
}
//(载入完成)send()方法执行完成,已经接收到全部响应内容
case 2:{
self.onLoaded();
break;
}
//交互)正在解析响应内容
case 3:{
self.onInteractive();
break;
}
//完成)响应内容解析完成,可以在客户端调用了
case 4:{
/*
1**:请求收到,继续处理
2**:操作成功收到,分析、接受
3**:完成此请求必须进一步处理
4**:请求包含一个错误语法或不能完成
5**:服务器执行一个完全有效请求失败
*/
switch(self.xmlhttp.status){
case 200 : {
self.responseText = self.xmlhttp.responseText;
self.responseXML = self.xmlhttp.responseXML;
break;
}
case 500 : {
self.onError();
break;
}
}
self.onCompletion();
break;
}
}
};
if(this.method=="post"){
this.xmlhttp.send(data); //发送请求
}else{
this.xmlhttp.send(); //发送请求
}
};
this.abort=function(){
this.xmlhttp.abort();
}
this.close=function(){
this.xmlhttp=null;
}
this.init();
}
// 将responseXML封装成数据集的类
function RecordSet(xml,recordName)
{
var records = xml.getElementsByTagName(recordName);
this.recordCount = records.length;
var current_index = 0;
var items = [];
if(this.recordCount > 0){
items = createFields(records[current_index]);
this.items = items;
}
//记录集下移
this.next = function(){
if(current_index < this.recordCount -1){
current_index += 1;
items = createFields(records[current_index]);
this.items = items;
}
}
//记录集上移
this.previous = function(){
if(current_index > 0){
current_index -= 1;
items = createFields(records[current_index]);
this.items = items;
}
}
//记录集上移至第一个记
this.first = function(){
current_index = 0;
if(this.recordCount > 0){
items = createFields(records[current_index]);
this.items = items;
}
}
//记录集上移至最后一个记录
this.last = function() {
current_index = this.recordCount - 1;
if(this.recordCount > 0){
items = createFields(records[current_index]);
this.items = items;
}
}
this.GotoRow = function(rowindex) {
if(rowindex == 0){
this.first();
}else{
if(this.recordCount > 0 && rowindex > 0 && rowindex < this.recordCount){
current_index = rowindex;
items = createFields(records[current_index]);
this.items = items;
}
}
}
}
// 构造记录的Field数组
function createFields(record){
var items = new Array();
var childNodes = record.childNodes;
for(var i=0;i<childNodes.length;i++){
var item = new Object();
item.name = childNodes.item(i).tagName;
item.value = childNodes.item(i).text;
items[items.length] = item;
items[item.name] = item;
}
return items;
}