聘我网

新概念招聘3.0

在Web开发中如何选择ContentType?

vote up0vote downstar

问题如下:

  1. 将json数据通过get/post的形式发往服务器,如何选择ContentType?
  2. 服务器将json数据推送至客户端如何选择ContentType?
  3. 服务器将js脚本推送至客户端如何选择ContentType?
 

1 个答复

vote up0vote downcheck

1、服务端 判断 客户端 提交的是否是 JSON数据 时 :

Content-Type = 'application/json;charset=UTF-8'
Content-Type = 'text/json;charset=UTF-8'
Content-Type = 'text/javascript;charset=UTF-8'
Content-Type = 'application/javascript;charset=UTF-8'

只要 Content-Type 满足上面4个条件中的任意一个时,就可以认为提交的数据是 JSON数据. 之所以要提供4种选择是因为 为了提供更好的兼容性. (我想没有人会提交真正的js代码到服务端然后用服务端js引擎去解析执行吧? 即使真有这种需求也可以在js代码外包一层 json格式的 wrapper , 所以姑且都当作json处理应该没什么问题)

2、服务端 向 客户端 发送 JSON数据 时:

Content-Type  = 'application/json;charset=UTF-8'

3、服务端 向 客户端 发送 JS 代码 时:

Content-Type = 'text/javascript;charset=UTF-8'
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.