I am having trouble encoding a response that I send back for an AJAX request using GZIP. Can someone give me some pointers to this, please?
- There is an AJAX request from JSP,
- The server-side action class (Struts) processes the request,
- The response is ready as a JSON object,
- The JSON string is written to the Response object and sent back,
- JSON string is read from responseText property of xmlHttp object back to jsp
It works great. However, instead of sending raw JSON data, if I send back encoded JSON data, problems arise.
Server code for creating GZip'ed JSON:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(jsonStr.getBytes());
gzip.close();
String newStr = new String(bos.toByteArray());
response.setHeader("Content-Type", "application/json");
response.setHeader("Content-Encoding", "gzip");
response.setHeader("Vary", "Accept-Encoding");
pw = response.getWriter();
pw.write(newStr);
pw.close();
In JSP:
alert('Length of the received Response Text : ' + xmlHttp.responseText.length);
jsonStr = eval('(' + xmlHttp.responseText + ')');
The notification field when receiving a response reports the length as 0!