自己动手封装的 ajax

时间:2021-05-18

以前开发用了很多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-ponents.join('&');
}
};
Form.Serializers = {
input: function(element) {
switch(element.type.toLowerCase()) {
case 'checkbox':
case 'radio':
return this.inputSelector(element);
default:
return this.textarea(element);
}
},
inputSelector: function(element) {
if(element.checked) {
return [element.name, element.value];
}
},
textarea: function(element) {
return [element.name, element.value];
},
select: function(element) {
return this[element.type == 'select-one' ?
'selectOne' : 'selectMany'](element);
},
selectOne: function(element) {
var value = null, option, index = element.selectedIndex;
if(index >= 0) {
option = element.options[index];
value = option.value == (undefined || '') ? option.text : option.value;
}
return [element.name, value];
},
selectMany: function(element) {
var value = [];
for(var i = 0; i < element.length; i++) {
var option = element.options[i];
if(option.selected) {
var optvalue = option.value == (undefined || '') ? option.text : option.value;
value.push(optvalue);
}
}
return [element.name, value];
}
};
function $F(element) {
this.form = element;
}
Object.extend($F.prototype, Form);
/**************************************************************
* 测试函数
*/
function onTest() {
//get提交方法
var params = new Object();
params.ss = '张三';
new ajax.request('ajax.do?method=doGet', {
onSuccess: function(transport) {
alert(transport.evalJSON().xx)
},
parameters: params
});
//post form提交方法
var params = new Object();
params.idd = 1000;
params.names = '张三'
new $F('form').request({
onSuccess: function(transport) {
alert(transport.evalJSON().xx);
},
parameters: params
});
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章