论坛首页 Web前端技术论坛

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

浏览 4296 次
精华帖 (1) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-01   最后修改:2009-07-01

在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对象一定要这么设定吗?

有点费解。

论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics