随笔 - 135  文章 - 1 评论 - 521 trackbacks - 9
<2006年2月>
2930311234
567891011
12131415161718
19202122232425
2627281234
567891011

搜索

 

常用链接

我参与的团队

随笔分类

随笔档案

相册

收藏夹

博客链接

技术站点

社区推荐

积分与排名

  • 积分 - 191765
  • 排名 - 315

最新评论

阅读排行榜

评论排行榜

过年啦,回到家一直不能上网,很是一个郁闷,不过大年廿九晚上闲来无事,终于解决了MagicAjax中文乱码的问题。


自从MA 0.2.x发行以来都不能支持JS的中文,比如把JS脚本中的那个"Loading ..."换成"数据加载中 ..."后,再运行时出现的那个条就是乱码。如果更改编码格式页面又会乱掉。

哈哈,下载MA的源代码包,打开MagicAjaxModule.cs文件,看到
        protected void Application_BeginRequest(object sender, EventArgs e)
        
{
            HttpContext context 
= ((HttpApplication)sender).Context;

            
// Init private fields
            _threadAbortExceptionThrown = false;
            _request 
= context.Request;
            _response 
= context.Response;


            
// Create a new context and add it to the items collection for later retrieval
            
// by MagicAjaxContext.Current
            _magicAjaxContext = new MagicAjaxContext();
            HttpContext.Current.Items.Add(MagicAjaxContext.ContextKey, _magicAjaxContext);

            
// Check if the request is for the embedded AjaxCallObject.js script
            if (context.Request.RawUrl.EndsWith("AjaxCallObject.js.aspx"))
            
{
                context.Response.ContentType 
= "text/javascript";
                
object cachedAjaxCallObjectJs = context.Cache["__CACHED_AJAXCALLOBJECT_JS"];
                
if (cachedAjaxCallObjectJs == null)
                
{
                    
//read and output the embedded AjaxCallObject.js file from the manifest
                    using (System.IO.StreamReader reader = new System.IO.StreamReader(typeof(MagicAjaxModule).Assembly.GetManifestResourceStream("MagicAjax.script.AjaxCallObject.js")))
                    
{
                        cachedAjaxCallObjectJs 
= reader.ReadToEnd();
                    }

                    context.Cache.Insert(
"__CACHED_AJAXCALLOBJECT_JS", cachedAjaxCallObjectJs);
                }

                context.Response.Write(cachedAjaxCallObjectJs);
                context.Response.Cache.SetExpires(DateTime.Now.AddYears(
1));
                context.Response.End();
            }

        }


噢,这样呀,让它支持GB2312不就行了。

        protected void Application_BeginRequest(object sender, EventArgs e)
        
{
            HttpContext context 
= ((HttpApplication)sender).Context;

            
// Init private fields
            _threadAbortExceptionThrown = false;
            _request 
= context.Request;
            _response 
= context.Response;
            _response.ContentEncoding 
= System.Text.Encoding.GetEncoding("GB2312");

            
// Create a new context and add it to the items collection for later retrieval
            
// by MagicAjaxContext.Current
            _magicAjaxContext = new MagicAjaxContext();
            HttpContext.Current.Items.Add(MagicAjaxContext.ContextKey, _magicAjaxContext);

            
// Check if the request is for the embedded AjaxCallObject.js script
            if (context.Request.RawUrl.EndsWith("AjaxCallObject.js.aspx"))
...

加入了_response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

编译Release文件,然后再使用这个新的DLL就OK了。


不知道,还有没有别的办法,或是以上方法都什么不当之处。

[仅就解决MA支持中文的问题]
0
0
(请您对文章做出评价)
« 上一篇:MagicAjax - 糟糕的文档不如不写文档
» 下一篇:有关ASP.Net 2.0发布后的问题
posted on 2006-02-08 12:15 搏软狂歌 阅读(3187) 评论(17)  编辑 收藏 网摘 所属分类: Ajax

FeedBack:
#1楼 2006-02-08 12:28 宝玉      
使js中的中文不乱码,很简单,使用unicode转义序列就好了
  回复  引用  查看    
#2楼 2006-02-08 12:28 Bruce Lee      
不错。可以
  回复  引用  查看    
#3楼 2006-02-08 14:41 永不言拜      
我直接改ajaxcallobject.js文件也可以呀。。
不过web.config要写上js的路径,我参考了以前一篇文章。。
下面是web.config的配制
<magicAjax scriptPath ="~/js/ajaxscript/"
outputCompareMode="HashCode"
tracing="false">
<pageStore
mode="NoStore"
unloadStoredPage="false"
cacheTimeout="5"
maxConcurrentPages="5"
maxPagesLimitAlert="false"


/>
</magicAjax>

  回复  引用  查看    
#4楼 2006-02-08 15:34 zygoses      
又一个中文问题
在将majicAjax面板中放置TextBox,修改它的Text值,取出来保存到数据库中,就成了乱码,不知还有什么其他方法,要改哪些地方?

  回复  引用  查看    
#6楼 2006-02-09 09:07 AlexHe[未注册用户]
把他的脚本文件改为utf-8文件格式,用写字版打开然后另存为就行了
  回复  引用    
#7楼 2006-02-23 19:08 ryanyang[未注册用户]
我使用AJAX后,有些页面会报语法错误,有遇到的吗
  回复  引用    
#8楼 2006-02-28 14:15 陌生海岸      
正在研究这个,可惜magicAjax的官方网站不能下载了。兄弟能否一个magicajax控件和源码给我呀。谢谢
owenye@game.optisp.com

  回复  引用  查看    
#9楼 2006-07-19 11:17 聂锋[未注册用户]
At 'AjaxCallObject.js', change the EncodePostData function either to:

AjaxCallObject.prototype.EncodePostData = function(data)
{
return escape(data).replace(/\+/ig, '%2B');
}

or to:

AjaxCallObject.prototype.EncodePostData = function(data)
{
return encodeURIComponent(data);
}

Test both of them and let us know if they work properly.


  回复  引用    
protected void Button1_Click(object sender, EventArgs e)
{
string sql;
sql = "select * from assess_rec where id>=1 and id<=5";
TextBox1.Text = sql;
}

把TextBox1,Button1都放在AjaxPanel1上就会出现上述问题,如果把Button1不放在AjaxPanel1上(但这样就会刷新了),TextBox1放在AjaxPanel1上就不会出现上述问题

  回复  引用    
这样的话,符号就被转义了!

  回复  引用    
#12楼 2007-01-09 16:25 maxell[未注册用户]
文章很好,可是官方网站下不了源文件了,你可以发个给我吗??谢谢了.
我的油箱:dearwdk@yeah.net

  回复  引用    
#13楼 2007-01-20 14:14 小月[未注册用户]
我也下不到源码 给个1.1可以用的好吗,谢谢。
trunkshkh@msn.com

  回复  引用    
#14楼 2007-01-29 19:26 ls0320[未注册用户]
我也下不了
LZ发个给我行吗?
ls0320@gmail.com

  回复  引用    
正在研究ajax,能否给我一份magicajax的代码,一直下载不了,
我要.net2.0版本的。谢谢!
zhouhaiquan2003@163.com

  回复  引用    
#16楼 2007-03-02 16:20 月[未注册用户]
也发一份给我吧1.1的 trunkshkh@msn.com
  回复  引用    
我也要一份2.0的zyyxg@126.com,谢谢
  回复  引用