模塊:dojo.io.IO
dojo.io.bind
處理請求取回需要的數據并處理
這個函數是AJAX中最為重要和有用的函數,dojo.io.bind這個類是用來處理客戶端與服務器間通訊的,需要通訊的參數由對象dojo.io.Request所定義,具體通訊的方法則由另外一個對象Transport所提供。
因此,我們如果需要與服務器通訊,則應該定義一個Request對象,其中包括服務器地址及回調函數,例子中Requset都是以匿名對象方式定義的
雖然我們可以定義一個自己的Transport,但是顯然不如直接利用現成的Transport方便。
Dojo里提供了一個同時兼容IE和Firefox的dojo.io.XMLHTTPTransport,但是這個對象位于dojo.io.BrowserIO,因此,一般require dojo.io.IO時,還應該require dojo.io.BrowserIO
Usage Example:
dojo.io.bind({
url: "http://localhost/test.html", //要請求的頁面地址
mimetype: "text/html", //請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype,默認為 "text/plain"
method:"GET", //默認為"GET"
sync: false, //默認為異步執行
useCache: false, //默認為不使用頁面緩存,注意這里的緩存并不是瀏覽器的緩存,而是Dojo自身所維護的頁面緩存
preventCache: false, //默認為啟用瀏覽器緩存,否則將通過自動增加不同的參數來確保瀏覽器緩存失效
timeoutSeconds: 3000, //3秒后超時,如果為0則永不超時
load: function(type, data, evt) { alert(data); }, //type should be "load", data is that we wanted
error: function(type, error) { alert(error.message); }, //error is dojo.io.Error
timeout: function(type) { alert("請求超時!"); }
});
你也可以用一個handle來處理所有的事件
dojo.io.bind({
url: "http://localhost/test.html", //要請求的頁面地址
mimetype: "text/html", //請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype
timeoutSeconds: 3000, //3秒后超時,如果為0則永不超時
handle: function(type, data, evt){
if(type == "load") { alert(data); } //data is that we wanted
else if (type == "error") { alert(data.message); } //data is the error object
else { ; } //other events maybe need handled
}
});
如果沒有在Request中指定所用的transport,則Dojo會自動的在已注冊的transports中尋找能夠處理這個Request的transport,如果不能找到,則返回指定的Request。下面是一個指定了transport的例子:
dojo.io.bind({
url: "http://localhost/test.html", //要請求的頁面地址
mimetype: "text/html", //請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype
timeoutSeconds: 3000, //3秒后超時,如果為0則永不超時
transport: "XMLHTTPTransport",
load: function(type, data, evt) { alert(data); }, //type should be "load", data is that we wanted
error: function(type, error) { alert(error.message); }, //error is dojo.io.Error
timeout: function(type) { alert("請求超時!"); }
});
你還可以利用bind來得到一個JavaScript所定義的對象(注意mimetype必須要定義為"text/javascript")
testObj = dojo.io.bind({
url: "http://localhost/test.js", //test.js里定義了一個對象
mimetype: "text/javascript", //請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype
timeoutSeconds: 3000, //3秒后超時,如果為0則永不超時
handle: function(type, data, evt){
if(type == "load") { alert(data); } //data is a object or value
else if (type == "error") { alert(data.message); } //data is the error object
else { ; } //other events maybe need handled
}
});
下面是一個Post的例子:
dojo.io.bind({
url: "http://localhost/test.aspx", //要提交的頁面地址
mimetype: "text/html", //請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype
timeoutSeconds: 3000, //3秒后超時,如果為0則永不超時
method: "POST",
formNode: dojo.byId("myForm"), //指定提交的Form名稱
load: function(type, data, evt) { alert(data); }, //type should be "load", data is that we wanted
error: function(type, error) { alert(error.message); }, //error is dojo.io.Error
timeout: function(type) { alert("請求超時!"); }
});
另一個Post的例子(without Form to post):
dojo.io.bind({
url: "http://localhost/test.aspx", //要提交的頁面地址
mimetype: "text/html", //請求的頁面的類型,應該設置為與你請求頁面類型對應的mimetype
timeoutSeconds: 3000, //3秒后超時,如果為0則永不超時
method: "POST",
content: {a: 1, b: 2}, //要提交的數據
load: function(type, data, evt) { alert(data); }, //type should be "load", data is that we wanted
error: function(type, error) { alert(error.message); }, //error is dojo.io.Error
timeout: function(type) { alert("請求超時!"); }
});
dojo.io.queueBind
有時,我們需要一次發出多個網頁請求,則應該使用dojo.io.queueBind,因為瀏覽器可能只允許同時發出有限個數的請求,如果是使用dojo.io.bind的話,則有可能會申請不到新的XMLHttp對象而導致出錯。
用法與dojo.io.bind是一樣的。
dojo.io.argsFromMap
用來把對象轉換為URL的參數形式
Usage Example:
dojo.io.argsFromMap({a:1,b:2,c:3}); //will return "c=3&b=2&a=1"
dojo.io.argsFromMap({name:"名稱",value:"值"},"utf"); //will return "value=%E5%80%BC&name=%E5%90%8D%E7%A7%B0", 有中文的話應該指定utf格式,否則dojo.string.encodeAscii返回的編碼是很怪異的
dojo.io.argsFromMap({a:1,b:2,c:3}, "utf", "c"); //will return "b=2&a=1&c=3",最后一個參數可以控制指定名稱的值出現在最后
dojo.io.setIFrameSrc
設置IFrame的Src
Usage Example:
dojo.io.setIFrameSrc(dojo.byId("myFrame"), "http://localhost/test.htm"); //myFrame打開指定的網頁
dojo.io.setIFrameSrc(dojo.byId("myFrame"), "http://localhost/test.htm", true); //myFrame打開指定的網頁,并覆蓋瀏覽器的歷史記錄
TODO: 補充一個kwArgs的例子,我之前在網上看見過,可是現在無論如何也找不到相關的頁面了,只好以后在舉例了
模塊:dojo.io.BrowserIO
基本上就提供了dojo.io.XMLHTTPTransport這個對象
XMLHTTPTransport一般能夠滿足我們的需求,但是其有幾個限制:它不能傳輸文件,不能夠成功執行跨域名的遠程請求,并且不支持 file:// 這樣的協議
因此,根據應用要求,我們可能會需要選用其它的transport: dojo.io.IframeTransport, dojo.io.repubsubTranport, dojo.io.ScriptSrcTransport, ShortBusTransport
dojo.io.IframeTransport,用法與xmlhttp是一樣的,其優點就是可以跨域,不存在任何的安全問題
如果Request指定的mimetype是text或javascript,返回的內容應該是放在第一個textarea里的內容,如果指定的mimetype是html,則IFrame里的html則是需要的內容。因為瀏覽器兼容的原因,IframeTransport不能正確處理返回類型為XML的請求。
關于Rpc,這個類似于Remoting的東西,也將在以后對其進行介紹。
posted on 2007-01-17 11:28
周銳 閱讀(282)
評論(0) 編輯 收藏 所屬分類:
Ajax