当前位置: 网学 > 编程文档 > Ajax > 正文

自己动手封装的 ajax

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/07/12

鉴于大家对Ajax十分关注,我们编辑小组在此为大家搜集整理了“自己动手封装的 ajax”一文,供大家参考学习

以前开发用了很多AJAX的技术比如EXT,prototype,jQuery等等,但都是开源封装好的AJAX框架。从没真正用过纯正的AJAX,故参照prototyp面向对象思想自己封装了一个AJAX框架。希望能给读者参考、帮助、评价。
复制代码 代码如下:
/*
* 自己封装的ajax
*
*
* @author 姜松
* @version 1.00 $date:2009-07-02
*
* history:
*
*/
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
Object.extend(String.prototype, {
include: function(pattern) {
return this.indexOf(pattern) > -1;
},
startsWith: function(pattern) {
return this.indexOf(pattern) == 0;
},
endsWith: function(pattern) {
return this.lastIndexOf(pattern) == (this.length - pattern.length);
},
empty: function() {
return /^\s*$/.test(this) || this == undefined || this == null;
}
});
Object.extend(Array.prototype, {
each: function(iterator) {
try {
for (var i = 0, length = this.length; i < length; i++) {
iterator(this[i]);
}
} catch (e) {
if (e != ''break'') { throw e };
}
},
clear: function() {
this.length = 0;
return this;
},
first: function() {
return this[0];
},
last: function() {
return this[this.length - 1];
},
indexOf: function(object) {
for (var i = 0, length = this.length; i < length; i++) {
if (this[i] == object) {return i};
}
return -1;
},
size: function() {
return this.length;
},
include: function(object) {
var found = false;
this.each(function(value) {
if (value == object) {found = true; throw ''break'';}
});
return found;
}
});
function $(element) {
if(arguments.length > 1) {
for(var i = 0, elements = [], length = arguments.length; i < length; i++) {
elements.push($(arguments[i]));
}
return elements;
}
if(typeof element == ''string'') {
element = document.getElementById(element);
}
return element;
};
var ajax = {
transport: new Object(),
options: new Object(),
getTransport: function() {
if(window.ActiveXObject) {
try {
return new ActiveXObject(''Msxm12.XMLHTTP'');
} catch(e) {
try {
return new ActiveXObject(''Microsoft.XMLHTTP'');
} catch(e) {}
}
} else if(window.XMLHttpRequest) {
try {
return new XMLHttpRequest();
} catch(e) {}
}
},
setOptions: function(options) {
ajax.options = {
method: ''get'',
asynchronous: true,
contentType: ''application/x-www-form-urlencoded'',
encoding: ''utf-8'',
parameters: ''''
};
Object.extend(ajax.options, options);
ajax.options.method = ajax.options.method.toUpperCase();
},
request: function(url, options) {
ajax.transport = ajax.getTransport();
ajax.setOptions(options);
this.method = ajax.options.method;
var params = ajax.options.parameters;
if (![''GET'', ''POST''].include(this.method)) {
this.method = ''GET'';
}
if (this.method == ''GET'') {
url = ajax.setParameters(url, params);
}
try {
ajax.transport.open(this.method, url, ajax.options.asynchronous);
ajax.transport.onreadystatechange = ajax.stateChange;
ajax.setRequestHeaders();
this.body = this.method == ''POST'' ? params : null;
ajax.transport.send(this.body);
} catch (e) {}
},
stateChange: function() {
try {
var readyS
  • 上一篇资讯: ajax poller
  • 网学推荐

    免费论文

    原创论文

    设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
    版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号