?dojo.io包很好的封裝了XMLHTTP和其它比較復(fù)雜的傳輸機(jī)制(iframe等)。另外"transports"傳輸器以插件形式(實(shí)現(xiàn)的統(tǒng)一的接口)出現(xiàn),另外dojo對(duì)于XMLHTTP的支持解決了back button的問題。我們先拋開細(xì)節(jié),看看dojo提供給我們的public API.
? ?dojo.io的許多不可思議的功能都由bind()方法來實(shí)現(xiàn)。(可能腳本語言或不定參數(shù)的語言都喜歡這樣,說實(shí)話,我不喜歡)。dojo.io.bind()是一個(gè)普通的匿名請(qǐng)求API,它的底層可以是不同的傳輸機(jī)制(queues of iframes, XMLHTTP, mod_pubsub, LivePage, 等)。dojo會(huì)嘗試選擇最好的傳輸機(jī)制,如果你的環(huán)境支持多種傳輸機(jī)制,XMLHTTP會(huì)是dojo的首選。bind()方法的參數(shù)是一個(gè)簡單的匿名對(duì)象,當(dāng)然這個(gè)對(duì)象的屬性是異常靈活的。
?如果你要得到一個(gè)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等差不多,簡單的代碼可以得到數(shù)據(jù)。可能你也想到,如果出現(xiàn)錯(cuò)誤怎么 辦,簡單注冊(cè)一個(gè)錯(cuò)誤處理函數(shù)就好了:
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也可以把所有的處理放到一個(gè)函數(shù)里,如下:
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經(jīng)常重服務(wù)器返回一段可執(zhí)行的javascript代碼段,在瀏覽器中eval(它)。你只要指定mimetype: "text/javascript" 就
可以了,dojo會(huì)自動(dòng)為你 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會(huì)覆蓋action.
dojo.io.bind({
? ?url: "http://foo.bar.com/process...,
? ?load: function(type, evaldObj){ /* do something */ },
? ?formNode: document.getElementById("formToSubmit")
});
呵呵,不錯(cuò)吧。下面看看幾個(gè)傳輸器。
Transports:
dojo.io.bind和其它相關(guān)函數(shù)與服務(wù)器通信有幾種方法,叫Transport,每種Transports可能都有缺陷,因此在特定場合你必須選擇合適的transport。
默認(rèn)的transport是大家熟悉的 XMLHttp.
XMLHttp
xmlhttp在大部分情況下工作的很好,但它不能上傳文件,不能跨域工作,也不能在file://協(xié)議下工作。(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.
這種響應(yīng)類型的url可以是text,html或者js/json.
? ?dojo.io的許多不可思議的功能都由bind()方法來實(shí)現(xiàn)。(可能腳本語言或不定參數(shù)的語言都喜歡這樣,說實(shí)話,我不喜歡)。dojo.io.bind()是一個(gè)普通的匿名請(qǐng)求API,它的底層可以是不同的傳輸機(jī)制(queues of iframes, XMLHTTP, mod_pubsub, LivePage, 等)。dojo會(huì)嘗試選擇最好的傳輸機(jī)制,如果你的環(huán)境支持多種傳輸機(jī)制,XMLHTTP會(huì)是dojo的首選。bind()方法的參數(shù)是一個(gè)簡單的匿名對(duì)象,當(dāng)然這個(gè)對(duì)象的屬性是異常靈活的。
?如果你要得到一個(gè)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等差不多,簡單的代碼可以得到數(shù)據(jù)。可能你也想到,如果出現(xiàn)錯(cuò)誤怎么 辦,簡單注冊(cè)一個(gè)錯(cuò)誤處理函數(shù)就好了:
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也可以把所有的處理放到一個(gè)函數(shù)里,如下:
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經(jīng)常重服務(wù)器返回一段可執(zhí)行的javascript代碼段,在瀏覽器中eval(它)。你只要指定mimetype: "text/javascript" 就
可以了,dojo會(huì)自動(dòng)為你 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會(huì)覆蓋action.
dojo.io.bind({
? ?url: "http://foo.bar.com/process...,
? ?load: function(type, evaldObj){ /* do something */ },
? ?formNode: document.getElementById("formToSubmit")
});
呵呵,不錯(cuò)吧。下面看看幾個(gè)傳輸器。
Transports:
dojo.io.bind和其它相關(guān)函數(shù)與服務(wù)器通信有幾種方法,叫Transport,每種Transports可能都有缺陷,因此在特定場合你必須選擇合適的transport。
默認(rèn)的transport是大家熟悉的 XMLHttp.
XMLHttp
xmlhttp在大部分情況下工作的很好,但它不能上傳文件,不能跨域工作,也不能在file://協(xié)議下工作。(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.
這種響應(yīng)類型的url可以是text,html或者js/json.