百度的Ajax.js文件(其實(shí)就在百度頁(yè)面上掛著呢……)
/*
Title: [Ajax.js簡(jiǎn)介]
文件名: ajax.js
版本:v1.0.0-100
版權(quán):(c)Baidu.com
作者:chenlin
簡(jiǎn)介:
> 這個(gè)文件是對(duì)xmlhttp異步請(qǐng)求進(jìn)行了簡(jiǎn)單的封裝,
> 主要是將prototytp進(jìn)行了簡(jiǎn)化,如果要使用復(fù)雜的功能可以使用prototype的ajax.js
> 外部使用時(shí),主要調(diào)用方式為
> var myAjax=new Ajax.Request(
> url,
> {
> method: 'get',
> asynchronous: true,
> onSuccess: function(xmlHttp)
> {
> },
> onFailure:function(xmlHttp){
> },
> onException:function(exception){
> }
> }
> );
> 另可使用myAjax.header()和myAjax.evalResponse();
> 其他的都為內(nèi)部調(diào)用函數(shù),外部盡量不要使用。Class: Ajax.Request類
*/
Function.prototype.bind = function(object) {
var __method = this;
return function() {
__method.apply(object, arguments);
}
};
/*
基礎(chǔ)ajax類
> 封裝了一個(gè)函數(shù):Ajax.getTransport()返回一個(gè)xmlhttp對(duì)象
> 這個(gè)類里的函數(shù)一般不使用。
*/
var Ajax = {
getTransport: function() {
return Try.these(
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
function() {return new XMLHttpRequest()}
) || false;
}
};
/*
基礎(chǔ)ajax.base類
> 封裝了三個(gè)函數(shù):
> setOptions:設(shè)置進(jìn)行ajax請(qǐng)求的參數(shù)
> responseIsSuccess:判斷異步請(qǐng)求返回是否成功
> responseIsFailure:判斷異步請(qǐng)求返回是否失敗
> 這個(gè)類里的函數(shù)也沒(méi)有必要在實(shí)際中使用,只有在擴(kuò)展是才會(huì)使用到
*/
Ajax.Base = function() {};
Ajax.Base.prototype = {
setOptions: function(options) {
this.options = {
method: 'post',//異步請(qǐng)求方法,可以為get和post,此處為默認(rèn)post
asynchronous: true, //設(shè)置是否為異步方式發(fā)送,
parameters: '' //傳遞參數(shù),參數(shù)都是url編碼格式a=valueOfA&b=valueOfB
}
Object.extend(this.options, options || {});//以上三個(gè)屬性為異步請(qǐng)求的基本屬性。
},
responseIsSuccess: function() {
return this.transport.status == undefined
|| this.transport.status == 0
|| (this.transport.status >= 200 && this.transport.status < 300);
},
responseIsFailure: function() {
return !this.responseIsSuccess();
}
};
/*
* 生成一個(gè)類Ajax.Request
*/
Ajax.Request = new Class();
/*
定義了xmlhttp請(qǐng)求的狀態(tài)。
> 0 (未初始化) 對(duì)象已建立,但是尚未初始化(尚未調(diào)用open方法)
> 1 (初始化) 對(duì)象已建立,尚未調(diào)用send方法
> 2 (發(fā)送數(shù)據(jù)) send方法已調(diào)用,但是當(dāng)前的狀態(tài)及http頭未知
> 3 (數(shù)據(jù)傳送中) 已接收部分?jǐn)?shù)據(jù),因?yàn)轫憫?yīng)及http頭不全,這時(shí)通過(guò)responseBody和responseText獲取部分?jǐn)?shù)據(jù)會(huì)出現(xiàn)錯(cuò)誤,
> 4 (完成) 數(shù)據(jù)接收完畢,此時(shí)可以通過(guò)通過(guò)responseBody和responseText獲取完整的回應(yīng)數(shù)據(jù)
*/
Ajax.Request.Events = ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
/*
*Ajax.Request對(duì)象,需要實(shí)例化對(duì)象,
*
*/
Ajax.Request.prototype = Object.extend(new Ajax.Base(),{
initialize: function(url, options) {
this.transport = Ajax.getTransport(); //返回xmlhttp對(duì)象
this.setOptions(options); //設(shè)置請(qǐng)求的參數(shù),在初始化時(shí)寫(xiě)
this.request(url); //進(jìn)行異步請(qǐng)求
},
request: function(url) {
var parameters = this.options.parameters || '';
if (parameters.length > 0) parameters += '&_=';
try {
this.url = url;
/*
* 如果是get方法的化,就將parameters里的內(nèi)容添加到url里
*/
if (this.options.method == 'get' && parameters.length > 0)
this.url += (this.url.match(/?/) ? '&' : '?') + parameters;
/*
* 調(diào)用xmlhttp的請(qǐng)求函數(shù),進(jìn)行請(qǐng)求
*/
this.transport.open(this.options.method, this.url,
this.options.asynchronous);
/*
* 如果是異步請(qǐng)求,對(duì)onreadystatechange綁定函數(shù),對(duì)于為什么要用定時(shí)器,沒(méi)10ms將狀態(tài)設(shè)為L(zhǎng)oading,我也沒(méi)有明白。
*/
if (this.options.asynchronous) {
this.transport.onreadystatechange = this.onStateChange.bind(this);
setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
}
this.setRequestHeaders(); //設(shè)置請(qǐng)求里的頭部信息,包括編碼等信息
/*
* 對(duì)與post方式請(qǐng)求,也可以通過(guò)options里的postBody來(lái)設(shè),這里就將postbody放到參數(shù)里進(jìn)行傳遞
*/
var body = this.options.postBody ? this.options.postBody : parameters;
/*
* 發(fā)送post數(shù)據(jù)
*/
this.transport.send(this.options.method == 'post' ? body : null);
} catch (e) {
this.dispatchException(e);
}
},
setRequestHeaders: function() {
var requestHeaders = ['X-Requested-With', 'XMLHttpRequest'];
if (this.options.method == 'post') {
requestHeaders.push('Content-type',
'application/x-www-form-urlencoded');
/* Force "Connection: close" for Mozilla browsers to work around
* a bug where XMLHttpReqeuest sends an incorrect Content-length
* header. See Mozilla Bugzilla #246651.
*/
if (this.transport.overrideMimeType)
requestHeaders.push('Connection', 'close');
}
if (this.options.requestHeaders)
requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);
for (var i = 0; i < requestHeaders.length; i += 2)
this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
},
/*
* 狀態(tài)改變時(shí),執(zhí)行respondToReadyState,判斷當(dāng)狀態(tài)為1時(shí)不執(zhí)行。
*/
onStateChange: function() {
var readyState = this.transport.readyState;
if (readyState != 1)
this.respondToReadyState(this.transport.readyState);
},
/*
* 返回異步請(qǐng)求返回文件的頭部信息,name表示名字,函數(shù)返回參數(shù)值
*/
header: function(name) {
try {
return this.transport.getResponseHeader(name);
} catch (e) {}
},
/*
*如果返回header信息里包括X-JSON/xxx,則會(huì)執(zhí)行xxx的內(nèi)容
*不是很明白這里,為什么要eval
*/
evalJSON: function() {
try {
return eval(this.header('X-JSON'));
} catch (e) {}
},
/*
* 很簡(jiǎn)單的函數(shù),如果返回Content-type為text/javascript,則會(huì)調(diào)用此函數(shù)進(jìn)行執(zhí)行,我認(rèn)為這處用途不大
*/
evalResponse: function() {
try {
return eval(this.transport.responseText);
} catch (e) {
this.dispatchException(e);
}
},
/*
*根據(jù)返回的狀態(tài)決定該執(zhí)行的步驟
*/
respondToReadyState: function(readyState) {
var event = Ajax.Request.Events[readyState];
var transport = this.transport, json = this.evalJSON();
if (event == 'Complete') {
/*
*此處如果狀態(tài)處于完成狀態(tài)則會(huì)進(jìn)行分析分別調(diào)用onSuccess和onFailure,所以一般onComplete不調(diào)用為好
*/
try {
(this.options['on' + this.transport.status]
|| this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')]
|| function(){})(transport, json);
} catch (e) {
this.dispatchException(e);
}
/*
* 此處對(duì)返回js進(jìn)行了eval
*/
if ((this.header('Content-type') || '').match(/^text/javascript/i))
this.evalResponse();
}
try {
(this.options['on' + event] || function(){})(transport, json);
} catch (e) {
this.dispatchException(e);
}
/*
* Avoid memory leak in MSIE: clean up the oncomplete event handler
*/
if (event == 'Complete')
this.transport.onreadystatechange = function(){};
},
/*
*錯(cuò)誤時(shí)調(diào)用,一般外部使用onException:function(){}
*/
dispatchException: function(exception) {
(this.options.onException || function(){})(this, exception);
}
}
);
/*
Title: [Ajax.js簡(jiǎn)介]
文件名: ajax.js
版本:v1.0.0-100
版權(quán):(c)Baidu.com
作者:chenlin
簡(jiǎn)介:
> 這個(gè)文件是對(duì)xmlhttp異步請(qǐng)求進(jìn)行了簡(jiǎn)單的封裝,
> 主要是將prototytp進(jìn)行了簡(jiǎn)化,如果要使用復(fù)雜的功能可以使用prototype的ajax.js
> 外部使用時(shí),主要調(diào)用方式為
> var myAjax=new Ajax.Request(
> url,
> {
> method: 'get',
> asynchronous: true,
> onSuccess: function(xmlHttp)
> {
> },
> onFailure:function(xmlHttp){
> },
> onException:function(exception){
> }
> }
> );
> 另可使用myAjax.header()和myAjax.evalResponse();
> 其他的都為內(nèi)部調(diào)用函數(shù),外部盡量不要使用。Class: Ajax.Request類
*/
Function.prototype.bind = function(object) {
var __method = this;
return function() {
__method.apply(object, arguments);
}
};
/*
基礎(chǔ)ajax類
> 封裝了一個(gè)函數(shù):Ajax.getTransport()返回一個(gè)xmlhttp對(duì)象
> 這個(gè)類里的函數(shù)一般不使用。
*/
var Ajax = {
getTransport: function() {
return Try.these(
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
function() {return new XMLHttpRequest()}
) || false;
}
};
/*
基礎(chǔ)ajax.base類
> 封裝了三個(gè)函數(shù):
> setOptions:設(shè)置進(jìn)行ajax請(qǐng)求的參數(shù)
> responseIsSuccess:判斷異步請(qǐng)求返回是否成功
> responseIsFailure:判斷異步請(qǐng)求返回是否失敗
> 這個(gè)類里的函數(shù)也沒(méi)有必要在實(shí)際中使用,只有在擴(kuò)展是才會(huì)使用到
*/
Ajax.Base = function() {};
Ajax.Base.prototype = {
setOptions: function(options) {
this.options = {
method: 'post',//異步請(qǐng)求方法,可以為get和post,此處為默認(rèn)post
asynchronous: true, //設(shè)置是否為異步方式發(fā)送,
parameters: '' //傳遞參數(shù),參數(shù)都是url編碼格式a=valueOfA&b=valueOfB
}
Object.extend(this.options, options || {});//以上三個(gè)屬性為異步請(qǐng)求的基本屬性。
},
responseIsSuccess: function() {
return this.transport.status == undefined
|| this.transport.status == 0
|| (this.transport.status >= 200 && this.transport.status < 300);
},
responseIsFailure: function() {
return !this.responseIsSuccess();
}
};
/*
* 生成一個(gè)類Ajax.Request
*/
Ajax.Request = new Class();
/*
定義了xmlhttp請(qǐng)求的狀態(tài)。
> 0 (未初始化) 對(duì)象已建立,但是尚未初始化(尚未調(diào)用open方法)
> 1 (初始化) 對(duì)象已建立,尚未調(diào)用send方法
> 2 (發(fā)送數(shù)據(jù)) send方法已調(diào)用,但是當(dāng)前的狀態(tài)及http頭未知
> 3 (數(shù)據(jù)傳送中) 已接收部分?jǐn)?shù)據(jù),因?yàn)轫憫?yīng)及http頭不全,這時(shí)通過(guò)responseBody和responseText獲取部分?jǐn)?shù)據(jù)會(huì)出現(xiàn)錯(cuò)誤,
> 4 (完成) 數(shù)據(jù)接收完畢,此時(shí)可以通過(guò)通過(guò)responseBody和responseText獲取完整的回應(yīng)數(shù)據(jù)
*/
Ajax.Request.Events = ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
/*
*Ajax.Request對(duì)象,需要實(shí)例化對(duì)象,
*
*/
Ajax.Request.prototype = Object.extend(new Ajax.Base(),{
initialize: function(url, options) {
this.transport = Ajax.getTransport(); //返回xmlhttp對(duì)象
this.setOptions(options); //設(shè)置請(qǐng)求的參數(shù),在初始化時(shí)寫(xiě)
this.request(url); //進(jìn)行異步請(qǐng)求
},
request: function(url) {
var parameters = this.options.parameters || '';
if (parameters.length > 0) parameters += '&_=';
try {
this.url = url;
/*
* 如果是get方法的化,就將parameters里的內(nèi)容添加到url里
*/
if (this.options.method == 'get' && parameters.length > 0)
this.url += (this.url.match(/?/) ? '&' : '?') + parameters;
/*
* 調(diào)用xmlhttp的請(qǐng)求函數(shù),進(jìn)行請(qǐng)求
*/
this.transport.open(this.options.method, this.url,
this.options.asynchronous);
/*
* 如果是異步請(qǐng)求,對(duì)onreadystatechange綁定函數(shù),對(duì)于為什么要用定時(shí)器,沒(méi)10ms將狀態(tài)設(shè)為L(zhǎng)oading,我也沒(méi)有明白。
*/
if (this.options.asynchronous) {
this.transport.onreadystatechange = this.onStateChange.bind(this);
setTimeout((function() {this.respondToReadyState(1)}).bind(this), 10);
}
this.setRequestHeaders(); //設(shè)置請(qǐng)求里的頭部信息,包括編碼等信息
/*
* 對(duì)與post方式請(qǐng)求,也可以通過(guò)options里的postBody來(lái)設(shè),這里就將postbody放到參數(shù)里進(jìn)行傳遞
*/
var body = this.options.postBody ? this.options.postBody : parameters;
/*
* 發(fā)送post數(shù)據(jù)
*/
this.transport.send(this.options.method == 'post' ? body : null);
} catch (e) {
this.dispatchException(e);
}
},
setRequestHeaders: function() {
var requestHeaders = ['X-Requested-With', 'XMLHttpRequest'];
if (this.options.method == 'post') {
requestHeaders.push('Content-type',
'application/x-www-form-urlencoded');
/* Force "Connection: close" for Mozilla browsers to work around
* a bug where XMLHttpReqeuest sends an incorrect Content-length
* header. See Mozilla Bugzilla #246651.
*/
if (this.transport.overrideMimeType)
requestHeaders.push('Connection', 'close');
}
if (this.options.requestHeaders)
requestHeaders.push.apply(requestHeaders, this.options.requestHeaders);
for (var i = 0; i < requestHeaders.length; i += 2)
this.transport.setRequestHeader(requestHeaders[i], requestHeaders[i+1]);
},
/*
* 狀態(tài)改變時(shí),執(zhí)行respondToReadyState,判斷當(dāng)狀態(tài)為1時(shí)不執(zhí)行。
*/
onStateChange: function() {
var readyState = this.transport.readyState;
if (readyState != 1)
this.respondToReadyState(this.transport.readyState);
},
/*
* 返回異步請(qǐng)求返回文件的頭部信息,name表示名字,函數(shù)返回參數(shù)值
*/
header: function(name) {
try {
return this.transport.getResponseHeader(name);
} catch (e) {}
},
/*
*如果返回header信息里包括X-JSON/xxx,則會(huì)執(zhí)行xxx的內(nèi)容
*不是很明白這里,為什么要eval
*/
evalJSON: function() {
try {
return eval(this.header('X-JSON'));
} catch (e) {}
},
/*
* 很簡(jiǎn)單的函數(shù),如果返回Content-type為text/javascript,則會(huì)調(diào)用此函數(shù)進(jìn)行執(zhí)行,我認(rèn)為這處用途不大
*/
evalResponse: function() {
try {
return eval(this.transport.responseText);
} catch (e) {
this.dispatchException(e);
}
},
/*
*根據(jù)返回的狀態(tài)決定該執(zhí)行的步驟
*/
respondToReadyState: function(readyState) {
var event = Ajax.Request.Events[readyState];
var transport = this.transport, json = this.evalJSON();
if (event == 'Complete') {
/*
*此處如果狀態(tài)處于完成狀態(tài)則會(huì)進(jìn)行分析分別調(diào)用onSuccess和onFailure,所以一般onComplete不調(diào)用為好
*/
try {
(this.options['on' + this.transport.status]
|| this.options['on' + (this.responseIsSuccess() ? 'Success' : 'Failure')]
|| function(){})(transport, json);
} catch (e) {
this.dispatchException(e);
}
/*
* 此處對(duì)返回js進(jìn)行了eval
*/
if ((this.header('Content-type') || '').match(/^text/javascript/i))
this.evalResponse();
}
try {
(this.options['on' + event] || function(){})(transport, json);
} catch (e) {
this.dispatchException(e);
}
/*
* Avoid memory leak in MSIE: clean up the oncomplete event handler
*/
if (event == 'Complete')
this.transport.onreadystatechange = function(){};
},
/*
*錯(cuò)誤時(shí)調(diào)用,一般外部使用onException:function(){}
*/
dispatchException: function(exception) {
(this.options.onException || function(){})(this, exception);
}
}
);

浙公網(wǎng)安備 33010602011771號(hào)