Tôi gặp sự cố using Gzip compression and JQuery together. Dường như nó có thể được gây ra bởi cách tôi gửi phản hồi JSON trong hành động Struts của tôi. Tôi sử dụng mã tiếp theo để gửi các đối tượng JSON của tôi trở lại.Làm thế nào để gửi JSON trở lại với JAVA?
public ActionForward get(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
JSONObject json = // Do some logic here
RequestUtils.populateWithJSON(response, json);
return null;
}
public static void populateWithJSON(HttpServletResponse response,JSONObject json) {
if(json!=null) {
response.setContentType("text/x-json;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
try {
response.getWriter().write(json.toString());
} catch (IOException e) {
throw new ApplicationException("IOException in populateWithJSON", e);
}
}
}
Có cách nào tốt hơn để gửi JSON trong ứng dụng web Java không?
Tôi đã sử dụng response.setContentType ("application/json; charset = UTF-8"); thay vì response.setContentType ("văn bản/x-json; charset = UTF-8"); – bentzy