Posted on 2008-05-24 16:06
Yidon 阅读(56)
评论(0) 编辑 收藏
不知道大家都是怎么使用Ajax技术的,现在Ajax框架类也不少。我都喜欢自己设计和编写代码,所以现成的Ajax框架我用的并不多,这类框架我就用过jQuery,我相信用过的人都知道它的强大,的确是一个非常好用的js类库,但是对它我总是觉得能少用就少用,能不用就不用,毕竟它功能太多了,体积自然就不小,而且我们常用到的功能可能都占不到它的十分之一,如果你像我一样,用jQuery只是为了使用它的Ajax框架的话,那么我建议你还是像我一样编个自己的框架吧。下面就是我编写的Ajax框架,代码不多,但我觉得对Ajax来说已经足够了,我做网站就差不多都用它。
以下就是基本代码,具体用法和更多解析请到我的U-Live 博客(http://blog.u-live.cn/blog-21.aspx)去查看,谢谢!

var ajax = function()
{};


ajax.CreateXMLHttp = function()
{

if(window.XMLHttpRequest)
{
return new XMLHttpRequest();

} else if(window.ActiveXObject)
{

try
{return new ActiveXObject("Microsoft.XMLHTTP");}

catch(e)
{

try
{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e)
{
alart("XMLHttp object could not be created.");
throw new Error("XMLHttp object could not be created.");
}
}
}
}

ajax.Request = function(url,func,isxml,ispost,parameters)


{
var xhr=this.CreateXMLHttp();
if(IsUndefined(ispost)) ispost=null;
if(ispost)

{
xhr.open("POST",url,true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
}
else
xhr.open("GET",url,true);

if(func)
{

xhr.onreadystatechange=function()
{

if(xhr.readyState == 4)
{
if(httpSuccess(xhr))
func(isxml&&xhr.responseXML?xhr.responseXML:xhr.responseText)
else
alert("请求页面发生错误");
}
}
}
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
if(!IsUndefined(parameters))
xhr.send(parameters);
else
xhr.send(null)
}

/**//*用Get方式提交数据*/
ajax.Get = function(url,func)


{
this.Request(url,func,false,false);
}

/**//*用Post方式提交数据*/
ajax.Post = function(url,func,parameters)


{
this.Request(url,func,false,true,parameters);
}