分享

dojo.io.bind的编码问题-cfans -JavaEye技术社区

 昵称22560 2007-03-21
关键字:   dojo bind 编码    

网摘记录一下,出处 http://www./blog/?p=18

----------------------
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:///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。

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多