关于ajax $.post() 返回乱码问题

一直出现乱码,关键在这里,请看这两段代码的区别

		Gson gson = new Gson();
		String str = gson.toJson(users);
		
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		PrintWriter out = response.getWriter(); 
		
		out.println(str);
		out.flush();




		Gson gson = new Gson();
		String str = gson.toJson(users);
		PrintWriter out = response.getWriter(); 
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		out.println(str);
		out.flush();


response.setContentType("application/json; charset=utf-8");//前者没有乱码,后者有乱码,关键在于这句话的位置

你可能感兴趣的:(Ajax,String)