在action层返回json数据有的人设置为 response.setContentType(“text/html”)
我看有的人返回json设置为response.setContentType(“text/json”)
我看有的人返回json设置为response.setContentType(“application/json”)
前台jsp页面 想返回json数据 在action层到底设置response.setContentType成什么
10分
输出的是文本,这几个都可以
这是给浏览器看的,如果你的json数据中有html标签,那第一种应该不行吧
10分
好像ie的话返回text/html这样的会把返回结果变成下载东西那样的
这个你随便开个网页就知道了,返回的contentType搜索下也行
好像挺多的类型
我 一直设置成 response.setContentType(“text/html”)
不过 response.setContentType(“application/json”)这样应该也可以
axaj才不管你返回的是什么类型的内容了,有json字符串,他可以解析就行
以前我都没注意过这个 学习下
引用 5 楼 wobuxiangnila 的回复:
axaj才不管你返回的是什么类型的内容了,有json字符串,他可以解析就行
你们项目怎么设置的啊
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
apolication/json; charset=UTF-8
text/html;charset=UTF-8
text/plain;charset=UTF-8
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
text/plain;charset=UTF-8
text/html;charset=UTF-8
引用 9 楼 wobuxiangnila 的回复:
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
text/plain;charset=UTF-8
text/html;charset=UTF-8
你上面这两种都可以吗
引用 8 楼 wobuxiangnila 的回复:
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
apolication/json; charset=UTF-8
text/html;charset=UTF-8
text/plain;charset=UTF-8
貌似我们项目就是这么配置的
引用 3 楼 scmod 的回复:
好像ie的话返回text/html这样的会把返回结果变成下载东西那样的
这个你随便开个网页就知道了,返回的contentType搜索下也行
好像挺多的类型
你们项目怎么配置的啊
application/json 是最标准的
引用 13 楼 yys79 的回复:
application/json 是最标准的
恩 我也觉得设置成 application/json 比较规范
application/json
response.setContentType(“text/html”);
response.setCharacterEncoding(“UTF-8”);
application/json
application/text都只是设置返回的http报文里面header的内容,最终怎么处理还是看前端javascript,你js里面收到数据后JSON.parse()就是json数据(当然现在Ajax框架比如jquery都自动parse了),不处理就是纯文本,本质上没什么差别