开发者

GZIP JSON AJAX response text is empty

开发者 https://www.devze.com 2022-12-11 07:49 出处:网络
I am facing a problem, while encoding the response that I send back for an AJAX request, using GZIP. Can anyone give me some pointers on this please?

I am facing a problem, while encoding the response that I send back for an AJAX request, using GZIP. Can anyone give me some pointers on this please?

  1. There is an AJAX request from the JSP,
  2. An action class (Struts) at the server side handles the request,
  3. The response is prepared as a JSON object,
  4. The JSON string is written to the Response object and sent back,
  5. the JSON string is read from the responseText property of the xmlHttp object back at the jsp

This works fine. However, instead of sending the raw J开发者_如何学编程SON data, if I send back encoded JSON data, then there are issues.

Server Side Code to create 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();

At the JSP :

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

The alert box, on receiving the response, reports length as 0!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号