在action层返回json数据设置为response.setContentType("text/html")的疑惑 各位大牛指点下啊

job920 2014-11-23 10:08:39
在action层返回json数据有的人设置为 response.setContentType("text/html")
我看有的人返回json设置为response.setContentType("text/json")
我看有的人返回json设置为response.setContentType("application/json")
前台jsp页面 想返回json数据 在action层到底设置response.setContentType成什么
...全文
23782 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsdf2008 2014-12-01
  • 打赏
  • 举报
回复
application/json
qingyuan18 2014-12-01
  • 打赏
  • 举报
回复
application/json application/text都只是设置返回的http报文里面header的内容,最终怎么处理还是看前端javascript,你js里面收到数据后JSON.parse()就是json数据(当然现在Ajax框架比如jquery都自动parse了),不处理就是纯文本,本质上没什么差别
dafengasp 2014-12-01
  • 打赏
  • 举报
回复
response.setContentType("text/html"); response.setCharacterEncoding("UTF-8");
job920 2014-11-30
  • 打赏
  • 举报
回复
引用 13 楼 yys79 的回复:
application/json 是最标准的
恩 我也觉得设置成 application/json 比较规范
loveunittesting 2014-11-25
  • 打赏
  • 举报
回复
application/json 是最标准的
猎魔人-不纯 2014-11-24
  • 打赏
  • 举报
回复
<!-- MVC Annotation Driven -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean id="utf8StringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/plain;charset=UTF-8</value>
						</list>
					</property>
				</bean>
				
				<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
			        <!-- 解决 HttpMediaTypeNotAcceptableException: Could not find acceptable representation -->
			        <property name="supportedMediaTypes">
			            <list>
			                <value>text/html;charset=UTF-8</value>
			            </list>
			        </property>
    			</bean>
    			
			</list>
		</property>
	</bean>
猎魔人-不纯 2014-11-24
  • 打赏
  • 举报
回复
<mvc:resources mapping="/resources/**" location="/resources/" />

	<!-- 采用SpringMVC自带的JSON转换工具,支持@ResponseBody注解 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>

	<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>apolication/json; charset=UTF-8</value>
				<value>text/html;charset=UTF-8</value>
				<value>text/plain;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	
job920 2014-11-24
  • 打赏
  • 举报
回复
引用 5 楼 wobuxiangnila 的回复:
axaj才不管你返回的是什么类型的内容了,有json字符串,他可以解析就行
你们项目怎么设置的啊
hadoop333 2014-11-24
  • 打赏
  • 举报
回复
以前我都没注意过这个 学习下
猎魔人-不纯 2014-11-24
  • 打赏
  • 举报
回复
axaj才不管你返回的是什么类型的内容了,有json字符串,他可以解析就行
jsdf2008 2014-11-24
  • 打赏
  • 举报
回复 1
我 一直设置成 response.setContentType("text/html") 不过 response.setContentType("application/json")这样应该也可以
job920 2014-11-24
  • 打赏
  • 举报
回复
引用 3 楼 scmod 的回复:
好像ie的话返回text/html这样的会把返回结果变成下载东西那样的 这个你随便开个网页就知道了,返回的contentType搜索下也行 好像挺多的类型
你们项目怎么配置的啊
jsdf2008 2014-11-24
  • 打赏
  • 举报
回复
引用 8 楼 wobuxiangnila 的回复:
<mvc:resources mapping="/resources/**" location="/resources/" />

	<!-- 采用SpringMVC自带的JSON转换工具,支持@ResponseBody注解 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
			</list>
		</property>
	</bean>

	<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>apolication/json; charset=UTF-8</value>
				<value>text/html;charset=UTF-8</value>
				<value>text/plain;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	
貌似我们项目就是这么配置的
job920 2014-11-24
  • 打赏
  • 举报
回复
引用 9 楼 wobuxiangnila 的回复:
<!-- MVC Annotation Driven -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean id="utf8StringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/plain;charset=UTF-8</value>
						</list>
					</property>
				</bean>
				
				<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
			        <!-- 解决 HttpMediaTypeNotAcceptableException: Could not find acceptable representation -->
			        <property name="supportedMediaTypes">
			            <list>
			                <value>text/html;charset=UTF-8</value>
			            </list>
			        </property>
    			</bean>
    			
			</list>
		</property>
	</bean>
你上面这两种都可以吗
slwsss 2014-11-23
  • 打赏
  • 举报
回复
输出的是文本,这几个都可以
scmod 2014-11-23
  • 打赏
  • 举报
回复
好像ie的话返回text/html这样的会把返回结果变成下载东西那样的 这个你随便开个网页就知道了,返回的contentType搜索下也行 好像挺多的类型
dokia123 2014-11-23
  • 打赏
  • 举报
回复
这是给浏览器看的,如果你的json数据中有html标签,那第一种应该不行吧

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧