ajax回调参数乱码怎么办( 二 )


1、解决的思惟本家儿如果经由过程领受数据 , 传输数据之前先利用utf-8编码解决 。
2、具体代码如下所示
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("application/json;charset=utf-8");
// setContentType与setCharacterEncoding的挨次不克不及更调 , 不然仍是无法解决中文乱码的问题
response.setCharacterEncoding("UTF-8");

Map<String,Object> map = new HashMap<String,Object>();
   map.put("name", "张三");
   map.put("name2", "李四");
   map.put("name3", "网二");
   JSONObject json = new JSONObject(map);
PrintWriter out =  response.getWriter();
out.write(json.toString());
out.close();
}
3、测试 。
3.1 打开页面 ,
3.2点击ajax按钮如下图所示 。

ajax回调参数乱码怎么办



ajax回调参数乱码怎么办



注重事项若是领受呈现乱码:new String(str.getBytes("ISO-8859-1"),"uft-8");

以上内容就是ajax回调参数乱码怎么办的内容啦 , 希望对你有所帮助哦!

猜你喜欢