GZIP JSON AJAX response text is empty

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:

// jsonStr = JSONObj.toString();  
ByteArrayOutputStream bos = new ByteArrayOutputStream();  
GZIPOutputStream gzip = new GZIPOutputStream(bos);  
gzip.write(jsonStr.getBytes());  
gzip.close();  
String newStr = new String(bos.toByteArray());  
// set the response header and send Encoded JSON response  
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:

// marker  
alert('Length of the received Response Text : ' + xmlHttp.responseText.length);
// evaluate the JSON  
jsonStr = eval('(' + xmlHttp.responseText + ')');

The notification field when receiving a response reports the length as 0!

+3

Source: https://habr.com/ru/post/1722982/


All Articles