---
title: "Extjs 中文乱码 "
author: "caodaoxi"
date: "2011-10-28T20:07:51Z"
tags: []
categories: ["Web前端"]
slug: "extjs"
draft: false
lastmod: "2011-10-28T20:07:51Z"
id: 1222131
url: "https://caodaoxi.iteye.com/blog/1222131"
---

Extjs 中文乱码

   我们在用Extjs做项目时，如果整个项目工程用的都是UTF-8编码的话，不论是Extjs项目或是别的项目，都不会不存在中文乱码问题，但是有的公司有的项目非要用GB2312或GBK的话那就会出现乱码问题，而你做为一个代码工人，只能去改了。  
因为ExtJS内部用的是UTF-8，所以与你项目用的GB2312或GBK编码冲突。我们要解决这个问题首先要保证我们的前后台编码一致，若是GB2312都用GB2312。

大体上有三个方面会出现中文乱码问题。  
1、grid数据列表展示打开时会报错，可能在tomcat下不会报错，我遇到的情况是在websphere下报错了，解决方法是，后台输出前设定输出编码，java中是response.setCharacterEncoding("GB2312");这样就可以解决了。  
2、store带参数加载数据，因为ext中的ajax用的是UTF-8，所以直接传的话会出现乱码，我们可以用Extjs的encodeURIComponent先把中文编码，然后在后台用java的java.net.URLDecoder再解码就可以解决，示例如下：  
stroe加载：  
store.baseParams={  
euser:encodeURIComponent(euser)  
batName:encodeURIComponent(batName)   //编码  
};  
store.load（）;  
后台解码：  
if(!Util.isEmpty(euser))  
euser=URLDecoder.decode(euser,"UTF-8");   //解码转中文  
if(!Util.isEmpty(batName))  
batName=URLDecoder.decode(batName,"UTF-8"); //解码转中文  
3、form表单提交，问题同上，解决方法也一样，我们要注意的就是给form表单中的中文何是编码，就是在form表单提交之前，示例如下：  
form提交：  
submit.on('click',function() {  
var batName=ECardForm.getForm().findField('batName').getValue();  
var userName=ECardForm.getForm().findField('userName').getValue();  
if(!Ext.isEmpty(batName))  
ECardForm.getForm().findField('batName').setValue(encodeURIComponent(batName));  
if(!Ext.isEmpty(userName))  
ECardForm.getForm().findField('userName').setValue(encodeURIComponent(userName))  
ECardForm.getForm().submit({  
success : function(form, action) {  
Ext.Msg.alert('信息', action.result.msg);  
},  
failure : function() {  
Ext.Msg.alert('错误', '系统出错，修改失败!');  
}  
});  
});  
后台解码同问题2.  
这样就可以解决extjs的中文乱码问题了