dojo.io.bind的编码问题

AJAX留言板今天增加了后台的删除功能,明天增加回复功能。

这个AJAX留言板目前用到了dojo,本来用原生Js也可以完全做到,但为了熟悉AJAX框架,开始采用dojo,权当练练手。使用中也确实发现,dojo确实省去了好多重复的东西,比如创建xhr对象等等,不过框架过于庞大,熟悉起来有一定难度,需要一个过程。

今天在调试时,看到有网友留言,显示内容为乱码;比较郁闷,因前几天因乱码问题已经解决了,为何又出现了呢?原来用的是原生JS,看来有可能是dojo的问题。

先简单介绍一下dojo.io模块

dojo.io.bind模块:处理请求并处理取回的数据,用法如下:

Usage Example:

dojo.io.bind({
url: “http://localhost/test.html“, //要请求的页面地址
mimetype: “text/html”, //请求的页面的类型,应该设置为与你请求页面类型对应的mimetype,默认为 “text/plain”,也可为”text/xml”
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(“请求超时!”); }
});

详细的使用方法回头整理一下再发出来吧。先接着说乱码的解决方法。

经过用google搜索”dojo.io.bind charset”,找到一篇文章,是dojo官方网站的maillist,地址在这里:http://dojotoolkit.org/pipermail/dojo-interest/2005-November/002156.html,标题是dojo.io.bind encoding。

文中写到:

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

作者遇到了象我一样的问题,即dojo.io.bind的编码问题。

回复如下(我就是根据这个方法解决的)
> > > 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”,
> > > >  …
> > > > }

即一个是在dojo.io.bind中加参数 encoding: “utf-8”,另一个是配置djConfig,增加一行:bindEncoding: “utf-8”,这两种方法我都分别试过,都可以。但这两个在官方的文档中好象没有说明。

上文中有一句话”the default is ASCII anyhow”,指dojo.io.bind默认的是ascii编码,应该是这个原因造成的,不知为何不默认为utf-8。

不管怎样终于解决了。编码问题总是比较烦人。

这两天累坏了,中午困得直磕睡;下午下班后小睡了一会儿,结果坐过了站,还好只过了一站地,走了回来。:) 要睡了,争取这周把这个留言板搞定。

现在如何实现不是问题,重要的是代码如何做到优雅,面向对象,高效,方便使用等等。

如果时间允许的话,会把这个留言板完善一下,比如分为PHP版,ASP版,纯JS版,dojo版,XML版,JSON版,分别按服务器端脚本、客户端脚本、数据格式分类,方便大家进行交流研究。

加油!

dojo.io.bind的编码问题》上有1条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注