我在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
各位还有什么高招吗?
试了各种办法,还是无效,待高手帮忙了
我用过的方法:
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
各位还有什么高招吗?
http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
request获取的时候
request.setCharacterEncoding("UTF-8");
我现在是要用responseText到.js文件(不是jsp文件),乱了
在那里看看有无能回答你的,谢谢,LZ,甭忘了给俺分哦,谢谢LZ
试试这个吧
string s = new string(request.getParameter("sssss").getbytes("iso8859-1"),"gb2312")
关键我现在return 之前还没乱,是到了js的时候responseText才乱
编码处理函数
1) encodeURI 返回一个对URI字符串编码后的结果。URL是最常见的一种URI;
2) decodeURI 将一个已编码的URI字符串解码成最原始的字符串返回;
若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;
}
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
加了这个反而好了,郁闷
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)