網(wǎng)摘記錄一下,出處 http://www.ajaxbbs.com/blog/?p=18 ---------------------- AJAX留言板今天增加了后臺(tái)的刪除功能,明天增加回復(fù)功能。 這個(gè)AJAX留言板目前用到了dojo,本來(lái)用原生Js也可以完全做到,但為了熟悉AJAX框架,開(kāi)始采用dojo,權(quán)當(dāng)練練手。使用中也確實(shí)發(fā)現(xiàn),dojo確實(shí)省去了好多重復(fù)的東西,比如創(chuàng)建xhr對(duì)象等等,不過(guò)框架過(guò)于龐大,熟悉起來(lái)有一定難度,需要一個(gè)過(guò)程。 今天在調(diào)試時(shí),看到有網(wǎng)友留言,顯示內(nèi)容為亂碼;比較郁悶,因前幾天因亂碼問(wèn)題已經(jīng)解決了,為何又出現(xiàn)了呢?原來(lái)用的是原生JS,看來(lái)有可能是dojo的問(wèn)題。 先簡(jiǎn)單介紹一下dojo.io模塊 dojo.io.bind模塊:處理請(qǐng)求并處理取回的數(shù)據(jù),用法如下: Usage Example: dojo.io.bind({ url: “http://localhost/test.html“, //要請(qǐng)求的頁(yè)面地址 mimetype: “text/html”, //請(qǐng)求的頁(yè)面的類型,應(yīng)該設(shè)置為與你請(qǐng)求頁(yè)面類型對(duì)應(yīng)的mimetype,默認(rèn)為 “text/plain”,也可為”text/xml” method:”GET”, //默認(rèn)為”GET” sync: false, //默認(rèn)為異步執(zhí)行 useCache: false, //默認(rèn)為不使用頁(yè)面緩存,注意這里的緩存并不是瀏覽器的緩存,而是Dojo自身所維護(hù)的頁(yè)面緩存 preventCache: false, //默認(rèn)為啟用瀏覽器緩存,否則將通過(guò)自動(dòng)增加不同的參數(shù)來(lái)確保瀏覽器緩存失效 timeoutSeconds: 3000, //3秒后超時(shí),如果為0則永不超時(shí) 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(”請(qǐng)求超時(shí)!”); } }); 詳細(xì)的使用方法回頭整理一下再發(fā)出來(lái)吧。先接著說(shuō)亂碼的解決方法。 經(jīng)過(guò)用google搜索”dojo.io.bind charset”,找到一篇文章,是dojo官方網(wǎng)站的maillist,地址在這里:http://dojotoolkit.org/pipermail/dojo-interest/2005-November/002156.html,標(biāo)題是dojo.io.bind encoding。 文中寫(xiě)到: Thank you Martin, Bob and David! I did some test, it looks like dojo.io.bind doesn’t support those two characters encoding. I think I will try to dig into the source code. If I find anythiing I’ll let you know. Siyan 作者遇到了象我一樣的問(wèn)題,即dojo.io.bind的編碼問(wèn)題。 回復(fù)如下(我就是根據(jù)這個(gè)方法解決的) > > > On Nov 28, 2005, at 7:33 PM, David Schontzler wrote: > > > > > > > Right now you can specify either UTF or ASCII encoding. Encoding > > > gets > > > > pretty hairy and we’ve tried to mimic the browser as much as > > > possible, > > > > but I probably missed something. You can try switching between the > > > two > > > > by setting encoding on the bind call: > > > > > > > > dojo.io.bind({ > > > > url: “…”, > > > > encoding: “utf-8″, > > > > … > > > > }); > > > > > > > > That’ll give you UTF-8 encoding (the default is ASCII anyhow). If > > > you > > > > want to set this option globally, you can do so in djConfig: > > > > > > > > djConfig = { > > > > bindEncoding: “utf-8″, > > > > … > > > > } 即一個(gè)是在dojo.io.bind中加參數(shù) encoding: “utf-8″,另一個(gè)是配置djConfig,增加一行:bindEncoding: “utf-8″,這兩種方法我都分別試過(guò),都可以。但這兩個(gè)在官方的文檔中好象沒(méi)有說(shuō)明。 上文中有一句話”the default is ASCII anyhow”,指dojo.io.bind默認(rèn)的是ascii編碼,應(yīng)該是這個(gè)原因造成的,不知為何不默認(rèn)為utf-8。 不管怎樣終于解決了。編碼問(wèn)題總是比較煩人。 |