?dojo.io包很好的封裝了XMLHTTP和其它比較復雜的傳輸機制(iframe等)。另外"transports"傳輸器以插件形式(實現的統一的接口)出現,另外dojo對于XMLHTTP的支持解決了back button的問題。我們先拋開細節,看看dojo提供給我們的public API.
? ?dojo.io的許多不可思議的功能都由bind()方法來實現。(可能腳本語言或不定參數的語言都喜歡這樣,說實話,我不喜歡)。dojo.io.bind()是一個普通的匿名請求API,它的底層可以是不同的傳輸機制(queues of iframes, XMLHTTP, mod_pubsub, LivePage, 等)。dojo會嘗試選擇最好的傳輸機制,如果你的環境支持多種傳輸機制,XMLHTTP會是dojo的首選。bind()方法的參數是一個簡單的匿名對象,當然這個對象的屬性是異常靈活的。
?如果你要得到一個text文件,可能代碼如下:

dojo.io.bind({
? ?url: "http://foo.bar.com/sampleD...,
? ?load: function(type, data, evt){ /*do something w/ the data */ },
? ?mimetype: "text/plain"
});

和prototype.js/YUI等差不多,簡單的代碼可以得到數據。可能你也想到,如果出現錯誤怎么 辦,簡單注冊一個錯誤處理函數就好了:

dojo.io.bind({
? ?url: "http://foo.bar.com/sampleD...,
? ?load: function(type, data, evt){ /*do something w/ the data */ },
? ?error: function(type, error){ /*do something w/ the error*/ },
? ?mimetype: "text/plain"
});


dojo也可以把所有的處理放到一個函數里,如下:

dojo.io.bind({
? ?url: "http://foo.bar.com/sampleD...,
? ?handle: function(type, data, evt){
? ? ? ?if(type == "load"){
? ? ? ? ? ?// do something with the data object
? ? ? ?}else if(type == "error"){
? ? ? ? ? ?// here, "data" is our error object
? ? ? ? ? ?// respond to the error here
? ? ? ?}else{
? ? ? ? ? ?// other types of events might get passed, handle them here
? ? ? ?}
? ?},
? ?mimetype: "text/plain"
});

考慮性能原因,Ajax經常重服務器返回一段可執行的javascript代碼段,在瀏覽器中eval(它)。你只要指定mimetype: "text/javascript" 就

可以了,dojo會自動為你 eval這些javascript代碼,

dojo.io.bind({
? ?url: "http://foo.bar.com/sampleD...,
? ?load: function(type, evaldObj){ /* do something */ },
? ?mimetype: "text/javascript"
});

你也可以指定你使用哪種傳輸器 如 XMLHTTPTransport:

dojo.io.bind({
? ?url: "http://foo.bar.com/sampleD...,
? ?load: function(type, evaldObj){ /* do something */ },
? ?mimetype: "text/plain", // get plain text, don't eval()
? ?transport: "XMLHTTPTransport"
});


Being a jack-of-all-trades, bind() also supports the submission of forms via a request (with the single caveat that it won't do file upload over XMLHTTP):
????得看看代碼?是否url會覆蓋action.


dojo.io.bind({
? ?url: "http://foo.bar.com/process...,
? ?load: function(type, evaldObj){ /* do something */ },
? ?formNode: document.getElementById("formToSubmit")
});

呵呵,不錯吧。下面看看幾個傳輸器。

Transports:
dojo.io.bind和其它相關函數與服務器通信有幾種方法,叫Transport,每種Transports可能都有缺陷,因此在特定場合你必須選擇合適的transport。
默認的transport是大家熟悉的 XMLHttp.

XMLHttp

xmlhttp在大部分情況下工作的很好,但它不能上傳文件,不能跨域工作,也不能在file://協議下工作。(dojo也代替的方案)

例子代碼:

<script type="text/javascript"> ? dojo.require("dojo.io.*"); ?
function mySubmit({ ? ?
? ?dojo.io.bind ({ ? ? ?
? ?url: 'server.cfm', ? ? ?
? ?handler: callBack, ? ? ? ? ?
? ?formNode: dojo.byId('myForm') ? ?
?}); ?
} ?
function callBack(type, data, evt) { ?
? ?dojo.byId('result').innerHTML = data; ?
}
</script>


IFrame I/O:
IFrame I/O transport是有用的,它可以上傳文件. ?例子代碼:


<script type="text/javascript"> ? dojo.require("dojo.io.*"); ?
dojo.require("dojo.io.IframeIO");
function mySubmit() { ? ?
?dojo.io.bind ({ ? ? ? ? ? url: 'server.cfm', ? ? ?
? ?handler: callBack, ? ? ?
? ?formNode: dojo.byId('myForm') ? ? ? }); ?
} ?
function callBack(type, data, evt) { ? ? ?
?dojo.byId('result').innerHTML = data; ?
}
</script>

The response type from the above URL can be text, html, or JS/JSON.
這種響應類型的url可以是text,html或者js/json.