在action层返回json数据设置为response.setContentType("text/html")

J2EE 码拜 9年前 (2015-04-05) 1624次浏览 0个评论
 

  在action层返回json数据有的人设置为 response.setContentType(“text/html”)
我看有的人返回json设置为response.setContentType(“text/json”)
我看有的人返回json设置为response.setContentType(“application/json”)
前台jsp页面 想返回json数据  在action层到底设置response.setContentType成什么

在action层返回json数据设置为response.setContentType("text/html")
10分
输出的是文本,这几个都可以
在action层返回json数据设置为response.setContentType("text/html")
这是给浏览器看的,如果你的json数据中有html标签,那第一种应该不行吧
在action层返回json数据设置为response.setContentType("text/html")
10分
好像ie的话返回text/html这样的会把返回结果变成下载东西那样的
这个你随便开个网页就知道了,返回的contentType搜索下也行
好像挺多的类型
在action层返回json数据设置为response.setContentType("text/html")
我 一直设置成   response.setContentType(“text/html”)
不过 response.setContentType(“application/json”)这样应该也可以
在action层返回json数据设置为response.setContentType("text/html")
axaj才不管你返回的是什么类型的内容了,有json字符串,他可以解析就行
在action层返回json数据设置为response.setContentType("text/html")
以前我都没注意过这个  学习下  
在action层返回json数据设置为response.setContentType("text/html")
引用 5 楼 wobuxiangnila 的回复:

axaj才不管你返回的是什么类型的内容了,有json字符串,他可以解析就行

你们项目怎么设置的啊

在action层返回json数据设置为response.setContentType("text/html")
<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>
在action层返回json数据设置为response.setContentType("text/html")
<!-- 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>
在action层返回json数据设置为response.setContentType("text/html")
引用 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>

你上面这两种都可以吗

在action层返回json数据设置为response.setContentType("text/html")
引用 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>

  貌似我们项目就是这么配置的

在action层返回json数据设置为response.setContentType("text/html")
引用 3 楼 scmod 的回复:

好像ie的话返回text/html这样的会把返回结果变成下载东西那样的
这个你随便开个网页就知道了,返回的contentType搜索下也行
好像挺多的类型

你们项目怎么配置的啊   

在action层返回json数据设置为response.setContentType("text/html")
application/json 是最标准的
在action层返回json数据设置为response.setContentType("text/html")
引用 13 楼 yys79 的回复:

application/json 是最标准的

恩   我也觉得设置成 application/json  比较规范

在action层返回json数据设置为response.setContentType("text/html")
application/json 
在action层返回json数据设置为response.setContentType("text/html")
response.setContentType(“text/html”);
     response.setCharacterEncoding(“UTF-8”);
在action层返回json数据设置为response.setContentType("text/html")
application/json
application/text都只是设置返回的http报文里面header的内容,最终怎么处理还是看前端javascript,你js里面收到数据后JSON.parse()就是json数据(当然现在Ajax框架比如jquery都自动parse了),不处理就是纯文本,本质上没什么差别

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明在action层返回json数据设置为response.setContentType("text/html")
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!