Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GZIP JSON AJAX响应文本为空_Ajax_Json_Struts_Gzip - Fatal编程技术网

GZIP JSON AJAX响应文本为空

GZIP JSON AJAX响应文本为空,ajax,json,struts,gzip,Ajax,Json,Struts,Gzip,在使用GZIP对针对AJAX请求发回的响应进行编码时,我遇到了一个问题。有人能给我一些关于这方面的建议吗 有一个来自JSP的AJAX请求 服务器端的操作类(Struts)处理请求 响应准备为JSON对象 JSON字符串被写入响应对象并发送回 JSON字符串从jsp上xmlHttp对象的responseText属性中读取 这个很好用。但是,如果我发回编码的JSON数据,而不是发送原始JSON数据,那么就会出现问题 创建GZip的JSON的服务器端代码: // jsonStr = JSONObj.t

在使用GZIP对针对AJAX请求发回的响应进行编码时,我遇到了一个问题。有人能给我一些关于这方面的建议吗

  • 有一个来自JSP的AJAX请求
  • 服务器端的操作类(Struts)处理请求
  • 响应准备为JSON对象
  • JSON字符串被写入响应对象并发送回
  • JSON字符串从jsp上xmlHttp对象的responseText属性中读取
  • 这个很好用。但是,如果我发回编码的JSON数据,而不是发送原始JSON数据,那么就会出现问题

    创建GZip的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();
    
    在JSP上:

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

    收到响应时,警报框报告长度为0

    你从哪里得到答复?您是否尝试在响应输出流中写入响应?响应的类型为HttpServletResponse。它作为函数execute的一个参数,位于org.apache.struts.action.action子类中!我使用的是Struts,那么你的动作类实现了ServletResponseAware?你们有并没有试着写响应输出流?嗯,事实上,我设法解决了。形成字符串时,字符集出现问题。将很快发布整个代码。希望这将有助于每个人了解发生了什么。感谢您对解决此问题的兴趣。响应是否如此之大,以至于您必须GZIP它?我问得更多是出于好奇,因为我想知道你是如何得出这个结论的,它需要GZIPping。