我在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
各位还有什么高招吗?

解决方案 »

  1.   

    ajax 发送request的时候设置: 
    http_request.setRequestHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
    request获取的时候
    request.setCharacterEncoding("UTF-8");
      

  2.   

    2楼的方法我已经试过了忘记和大家说一下了,我在传回之前java控制台看还没有乱
      

  3.   

    AJAX和页面概念一样直接页面访问是乱码吗?
      

  4.   

    我通过responseXML到jsp是不乱的
    我现在是要用responseText到.js文件(不是jsp文件),乱了
      

  5.   

    按有几个群,你不妨加进去,可以和大家一起讨论啊.........46986340,28039577,4804620                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
    在那里看看有无能回答你的,谢谢,LZ,甭忘了给俺分哦,谢谢LZ
      

  6.   


    试试这个吧
    string s = new string(request.getParameter("sssss").getbytes("iso8859-1"),"gb2312")
      

  7.   

    ls是在java端乱了才改啊
    关键我现在return 之前还没乱,是到了js的时候responseText才乱
      

  8.   

    LZ用用JAVASCRIPT中的编码函数试试
    编码处理函数 
    1) encodeURI 返回一个对URI字符串编码后的结果。URL是最常见的一种URI;
     2) decodeURI 将一个已编码的URI字符串解码成最原始的字符串返回;
      

  9.   

    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;
    }
      

  10.   

    response.setCharactorEncoding("UTF-8");放在程序的最前面
      

  11.   

    感谢各位,结帖了,最后成功的竟然不是这样
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    加了这个反而好了,郁闷
      

  12.   

    PrintWriter javax.servlet.ServletResponse.getWriter() throws IOExceptiongetWriter
    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)