Ajax request response 乱码解决方法

2019-09-14 07:19:04刘景俊

    } catch (Exception e) {
        e.printStackTrace();
    }
} else if ("getPageToolBar".equals(actParam)) {
    PaginationEntity pEntity = (PaginationEntity) sessionObj.getAttribute("paginationEntity");
    String tempStr = pEntity.getPaginationTool();
    OutputStream out;
    try {
        response.setContentType("text/html;charset=UTF-8");
        out = response.getOutputStream();
        out.write(tempStr.getBytes("UTF-8"));
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

因为,目前使用xmlHttpObj.responseText,responseText默认就是UTF-8所以将跳转的页面弄成UTF-8是不错的选择。至少不用担心乱码。。
[乱码原因]
之前此功能使用的是GB18030编码,到修改为止在其它客户机以及同事的机器上都非常正常,但就是我自己的机器翻页出现乱码。当我将此功能所用到的呈现文件统一使用UTF-8就ok了,也不知那xmlHttpObj触动了机器的那个神经。。查了些资料也都是说搞国际化的趋势,问题是解决了但原因没有找到!
[发现一Bug]
response.setContentType("text/html;charset=UTF8");在我机器上不抛错,但在同事的机器上就会抛错
改成response.setContentType("text/html;charset=UTF-8");一切OK..