和在我以前使用這個類庫的不少開發人員一樣,一開始,我不得不一頭紮進閱讀prototype.js的原始碼和實驗它的功能中。Prototype中的ajax.js提供了一個非常好用的ajax架構,一般應用中簡單的調用以下代碼就可以了
new Ajax.Request(
url, {method: “get”,
onSuccess: showFilter,
onFailure: function(request){alert(”Server error!”)},
onException: showError}
);
這個架構中提供了如下的對象和方法等:
Ajax對象:只有一個getTransport方法,返回一個XMLHttpRequest對象,另外有一個activeRequestCount屬性,反映當前正在處理的ajax數量
Ajax.Responders對象:繼承自Enumerable,管理全域Ajax的請求,具有如下方法:
register(responder):註冊一個管理ajax請求的對象
unregister(responder):撤銷一個管理ajax請求的對象
dispatch(callback, request, transport, json):觸發註冊的處理對象的方法
這個對象一般很少使用,系統中已經使用如下的代碼註冊了一個處理對象
Ajax.Responders.register({
onCreate: function() {
Ajax.activeRequestCount++;
},
onComplete: function() {
Ajax.activeRequestCount–;
}
});
Ajax.Base類:
Ajax的基類, 只有一個方法setOptions(options), 預設request參數如下,你可以在建立Ajax.request時指定:
method:'post’,
asynchronous: true,
contentType:'application/x-www-form-urlencoded’,
encoding:'UTF-8′,