Ajax 简单封装
2008-09-02 20:30
295 查看
/*********************************************************************************************
Ajax类应用示例代码:
new Ajax({
url: 'readMessageList.action',
onComplete: function(data){document.getElementById('message_div').innerHTML += data;}
}).doRequest();
**********************************************************************************************/
Ajax = function(config)
{
this._xmlHttpRequest = null;
this._userConfig = config;
this._config = {
method: 'get', // get||post|| ...
async: true,
returnDataFormat: 'jsCode', // jsCode||string||xml
url: null,
onComplete: function(data){}
};
this._init = function()
{
if (window.XMLHttpRequest) {
this._xmlHttpRequest = window.XMLHttpRequest;
}
else {
this._xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
};
this.doRequest = function()
{
var instance = this;
if (!this._xmlHttpRequest) {
this._init();
}
if (this._userConfig) {
for (var key in this._userConfig) {
this._config[key] = this._userConfig[key];
}
}
this._xmlHttpRequest.open(this._config.method, this._config.url, this._config.async);
this._xmlHttpRequest.onreadystatechange = function()
{
if (instance._xmlHttpRequest.readyState == 4){
if (instance._xmlHttpRequest.status == 200){
var responseText = instance._xmlHttpRequest.responseText;
if (instance._config.returnDataFormat == 'jsCode') {
instance._config.onComplete(eval('(' + responseText + ')'));
}
else if (instance._config.returnDataFormat == 'string') {
instance._config.onComplete(responseText);
}
else if (instance._config.returnDataFormat == 'xml'){
instance._config.onComplete(instance._xmlHttpRequest.responseXml);
}
}
}
};
instance._xmlHttpRequest.send(null);
}
}
Ajax类应用示例代码:
new Ajax({
url: 'readMessageList.action',
onComplete: function(data){document.getElementById('message_div').innerHTML += data;}
}).doRequest();
**********************************************************************************************/
Ajax = function(config)
{
this._xmlHttpRequest = null;
this._userConfig = config;
this._config = {
method: 'get', // get||post|| ...
async: true,
returnDataFormat: 'jsCode', // jsCode||string||xml
url: null,
onComplete: function(data){}
};
this._init = function()
{
if (window.XMLHttpRequest) {
this._xmlHttpRequest = window.XMLHttpRequest;
}
else {
this._xmlHttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
};
this.doRequest = function()
{
var instance = this;
if (!this._xmlHttpRequest) {
this._init();
}
if (this._userConfig) {
for (var key in this._userConfig) {
this._config[key] = this._userConfig[key];
}
}
this._xmlHttpRequest.open(this._config.method, this._config.url, this._config.async);
this._xmlHttpRequest.onreadystatechange = function()
{
if (instance._xmlHttpRequest.readyState == 4){
if (instance._xmlHttpRequest.status == 200){
var responseText = instance._xmlHttpRequest.responseText;
if (instance._config.returnDataFormat == 'jsCode') {
instance._config.onComplete(eval('(' + responseText + ')'));
}
else if (instance._config.returnDataFormat == 'string') {
instance._config.onComplete(responseText);
}
else if (instance._config.returnDataFormat == 'xml'){
instance._config.onComplete(instance._xmlHttpRequest.responseXml);
}
}
}
};
instance._xmlHttpRequest.send(null);
}
}
相关文章推荐
- Extjs读取更改或者发送ajax返回请求的结果简单封装
- html ajax 简单封装
- 简单封装ajax的get和post请求
- ajax提交封装,简单仿Jquery
- javascript中Ajax的简单封装
- 模拟jQuery简单封装ajax
- 简单ajax封装
- 自己写的封装好的简单的AJAX--javascript (改良版)
- ajax代码及简单封装
- 自用的简单封装ajax,试用初学者
- 简单的AJAX封装的三级联动
- Ajax Zero to Hero (2)入门代码的简单封装
- javascript实现简单的ajax封装示例
- 简单封装$.ajax() 方法
- ajax简单封装
- 一个简单的ajax封装
- apicloud简单封装ajax
- Ajax的简单封装
- ajax简单的封装一下
- ajax简单封装