xiashenghai 发表于 2013-1-28 19:26:37

设置response的"contentType"属性以便Prototype可以自动识别为Json对象

在ajax操作成功之后,系统要求给回显一些信息给用户,譬如,用户在重置密码后,要将新密码回显给用户。
 
这是这样实现的,
/** * 直接将字Json的字符串写入到Response中 ** @param response * @param jsonString Json的字符串 */public void responseJsonString(HttpServletResponse response,    String jsonString) {      response.setContentType(contentType);      byte[] bytes;      try {            bytes = jsonString.getBytes(charsetName);      } catch (UnsupportedEncodingException e) {            throw new UncheckedException(e);      }      response.setContentLength(bytes.length);      try {            OutputStream os = response.getOutputStream();            os.write(bytes);            os.flush();            if (logger.isDebugEnabled()) {      logger.debug("往客户端发送的Json串为:" + jsonString);            }      } catch (IOException e) {            logger.error("在往Response中写入Json字符串的时候发生异常。", e);            throw new UncheckedException(e);      }      } 
对于response.setContentType(contentType);其中contentType已经注入了值,看下注入的是什么值,
<!-- 这种设置Prototype可以自动识别为Json对象 --><property name="contentType"><value>application/x-javascript;charset=UTF-8</value></property><property name="charsetName"><value>UTF-8</value></property> prototype识别Json对象一定要这么设定吗?
有点费解。
页: [1]
查看完整版本: 设置response的"contentType"属性以便Prototype可以自动识别为Json对象