当前位置: 首页 > 图文教程 > 网络编程 > AJAX技术 > 一个封装的Ajax类

AJAX技术
一个简单的ASP+AJAX留言本源码下载
IE7下ajax之open Method New的说明
ASP+Ajax实现无刷新评论简单例子
AJAX的阻塞及跨域名解析
[js]一个获取页面ip的正则
AJAX乱码解决新方法
也写一个Ajax.Request类附代码
AJAX简历系统附js文件
Ajax留言本源码 提供下载了
找到一款不错的基于AJAX留言板源码(PHP版、ASP版)提供下载了
Ajax 学习资源 中外都有
本人ajax留言板的源程序 不错的应用js
xmlhttp 乱码 比较完整的解决方法 (UTF8,GB2312 编码 解码)
AJAX集天气\IP\多国语言翻译MP3(可同步LRC歌词显示)\万年历查询通
AJAX缓存问题的两种解决方法(IE)
AJAX 常用函数创建XMLHTTP对象,区别IE,Mozilla浏览器
Ajax的小贴士使用小结
用ajax动态加载需要的js文件
XMLHTTP多浏览器兼容性写法
PJBLOG中用到的ajaxjs.几个简单的函数

AJAX技术 中的 一个封装的Ajax类


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-13   浏览: 44 ::
收藏到网摘: n/a

用法:
new Ajax().Request(url,cmd,async,method,postString,title)
参数:
url: 请求页面URL(必填)
cmd: 返回值处理函数(必填)
async: 是否异步 ,(ture|false), 默认true
method: 请求方式,(post|get), 默认get
postString: 请求方式为post时,请求内容
title: 请求内容标题

复制代码 代码如下:

// Ajax 封装 2007-3-13
function createXMLHttpRequest() {
try {
if (window.XMLHTTPRequest) {
return new XMLHttpRequest();
}
else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");}
}
function messageDiv(t)
{
var v = document.createElement("<div>");
v.innerHTML = "<table style=\"width:300px;\" id=message>" +
"<tr style=\"font-size:12px;background-color:#EEEEff;color:#227933;height:20px\">" +
"<td style=\"padding:2px;border-top:1px solid #E1E1E1;border-left:1px solid #E1E1E1;border-bottom:1px solid #818181;border-right:1px solid #A1A1A1\">" +
"<nobr><img src=refresh.gif align=absmiddle> " + t + ",<span id=Span1>连接未初始化...</span></nobr></td></tr></table>";
var l = document.getElementsByName("message").length;
v.style.cssText = "position:absolute;bottom:" + (l*24) + "px;left:0px;display:none";
document.body.appendChild(v);
this.clear = function () {
document.body.removeChild(v);
var msg = document.getElementsByName("message");
for (var i=0;i<msg.length;i++){
msg[i].parentNode.style.cssText = "position:absolute;bottom:" + (i*24) + "px;left:0px";
}
}
this.showmsg = function (s) {
v.style.display = "";
v.all.Span1.innerHTML = s;
}
}
function Ajax() {
var x = new createXMLHttpRequest();
this.Request = function (url,cmd,async,method,postString,title) {
if (method!="post") method = "post"; else method = "get";
if (async!=true) async = true; else async = false;
if (typeof(postString)!="string") postString="";
if (typeof(title)!="string") title="正在获取数据"; else title="正在获取" + title;
var msgbox = new messageDiv(title);
x.onreadystatechange = function ()
{
if (async) switch (x.readystate) {
case 1:
msgbox.showmsg("正在初始化连接...");
return;
case 2:
msgbox.showmsg("正在发送数据...");
return;
case 3:
msgbox.showmsg("正在接收数据...");
return;
case 4:
msgbox.showmsg("数据接收完成...");
if (x.status == 200) {
cmd(x.responseText);
msgbox.clear();
}
else {
msgbox.showmsg("请求失败," + x.statustext + "(" + x.status + ")");
setTimeout(msgbox.clear,3000);
}
return;
}
}
x.open (method,url,async);
if (method=="post") {msgbox.showmsg("正在接收数据...");x.send(postString);} else x.send();
if (!async) {
msgbox.showmsg("数据接收完成...");
cmd(x.responseText);
msgbox.clear();
}
}
}