大清早发个中文乱码问题,大家帮帮忙

sunwei_07 2008-01-15 08:56:32
我在AJAX里在.js里用json接受responseText的值。出现了传说中的乱码问题,中文全部显示?号
试了各种办法,还是无效,待高手帮忙了
我用过的方法:
response.setHeader("Charset","GB2312");/response.setHeader("Charset","UTF-8");

response.setContentType("text/json; charset=utf-8"); /response.setContentType("text/json; charset=GB2312");

response.setContentType("text/xml; charset=utf-8");/response.setContentType("text/xml; charset=GB2312");

JSP文件:<%@page contentType="text/html;charset=GB2312"%>或者UtF-8

<meta http-equiv="Content-Type" content="text/html;charset=utf-8">或者gb2312


各位还有什么高招吗?


...全文
260 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
李文 2008-06-23
  • 打赏
  • 举报
回复
PrintWriter javax.servlet.ServletResponse.getWriter() throws IOException

getWriter
PrintWriter getWriter()
throws IOException
Returns a PrintWriter object that can send character text to the client. The PrintWriter uses the character encoding returned by getCharacterEncoding(). If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.
Calling flush() on the PrintWriter commits the response.

Either this method or getOutputStream() may be called to write the body, not both.


Returns:
a PrintWriter object that can return character data to the client
Throws:
UnsupportedEncodingException - if the character encoding returned by getCharacterEncoding cannot be used
IllegalStateException - if the getOutputStream method has already been called for this response object
IOException - if an input or output exception occurred
See Also:
getOutputStream(), setCharacterEncoding(java.lang.String)
sunwei_07 2008-01-25
  • 打赏
  • 举报
回复
感谢各位,结帖了,最后成功的竟然不是这样
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
加了这个反而好了,郁闷
Mars酱 2008-01-24
  • 打赏
  • 举报
回复
为什么不试试GBK编码?
老紫竹 2008-01-22
  • 打赏
  • 举报
回复
response.setCharactorEncoding("UTF-8");
放在程序的最前面
hwjfshx 2008-01-21
  • 打赏
  • 举报
回复
JS是什么编码格式?
若JS文件编码格式不一致,也能导致乱码(用Eclipse或DW等工具都能更改文件的编码格式)

如果是查询字符串乱码:
/**
* 字符内码转换 ISO TO GBK
*/
public static String getISOtoGBK(String param){
if (param == null || "".equals(param)){
return "";
}
param = param.trim();
String strtmp = "";
try{
strtmp = new String(param.getBytes("iso-8859-1"), "GBK");
}catch(Exception ex){
ex.printStackTrace();
return "";
}
return strtmp;
}
/**
* 字符内码转换 GBKTOISO
*/
public static String getGBKtoISO(String para) {
if (para == null || "".equals(para)) {
return "";
}
para = para.trim();
byte[] tempstr;
String utf_str = new String("");
try {
tempstr = para.getBytes("GBK");
utf_str = new String(tempstr, "ISO-8859-1");
}
catch (Exception ex) {
return "";
}
return utf_str;
}
sunwei_07 2008-01-21
  • 打赏
  • 举报
回复
具体怎么用呢
showtime520 2008-01-21
  • 打赏
  • 举报
回复
LZ用用JAVASCRIPT中的编码函数试试
编码处理函数
1) encodeURI 返回一个对URI字符串编码后的结果。URL是最常见的一种URI;
2) decodeURI 将一个已编码的URI字符串解码成最原始的字符串返回;
linyuepeng 2008-01-21
  • 打赏
  • 举报
回复
URLEcoder和URLDecoder
sunwei_07 2008-01-18
  • 打赏
  • 举报
回复
ls是在java端乱了才改啊
关键我现在return 之前还没乱,是到了js的时候responseText才乱
wangdeli501 2008-01-17
  • 打赏
  • 举报
回复

试试这个吧
string s = new string(request.getParameter("sssss").getbytes("iso8859-1"),"gb2312")
sunwei_07 2008-01-17
  • 打赏
  • 举报
回复
还是不行,各位大侠,还有高招么?
zen_gwen 2008-01-16
  • 打赏
  • 举报
回复
你用iso8859_1编码试试
hahe7788 2008-01-15
  • 打赏
  • 举报
回复
按有几个群,你不妨加进去,可以和大家一起讨论啊.........46986340,28039577,4804620
在那里看看有无能回答你的,谢谢,LZ,甭忘了给俺分哦,谢谢LZ
sunwei_07 2008-01-15
  • 打赏
  • 举报
回复
自己up
sunwei_07 2008-01-15
  • 打赏
  • 举报
回复
我通过responseXML到jsp是不乱的
我现在是要用responseText到.js文件(不是jsp文件),乱了
KK3K2005 2008-01-15
  • 打赏
  • 举报
回复
AJAX和页面概念一样

直接页面访问是乱码吗?
sunwei_07 2008-01-15
  • 打赏
  • 举报
回复
2楼的方法我已经试过了

忘记和大家说一下了,我在传回之前java控制台看还没有乱
sun_3211 2008-01-15
  • 打赏
  • 举报
回复
mark
c2one 2008-01-15
  • 打赏
  • 举报
回复
ajax 发送request的时候设置:
http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
request获取的时候
request.setCharacterEncoding("UTF-8");

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧