Return JSON from servlet
I was having the same problem. It was working well on Firefox but not on IE... I found out reading this post that my problem was related to the 'Content-Type'. The problem seems that that IE has problem with 'charset=UTF8'. However, if you use 'charset=UTF-8' (with a dash) it is then working! Your Content-Type should then be: application/json;charset=UTF-8
IE caches AJAX requests aggressively (more than Firefox, Chrome, and Safari, anyway).
Sometimes you need to set cache header controller when request. Like cache:false
. I tried to fix your code like this
request.setCharacterEncoding("utf8");
//response.setCharacterEncoding("utf8");
response.setContentType("application/json");
PrintWriter out = response.getWriter();
JSONObject jsonObj = (JSONObject) JSONValue.parse(request.getParameter("para"));
System.out.println(jsonObj.get("message"));
JSONObject obj = new JSONObject();
obj.put("message", "hello from server");
out.print(obj.toString());
I changed your response content-type from application/json; charset=utf8
to just application/json
and that worked.