Ajax应用常见的HTTP ContentType设置

原文出处不详,anyway,感谢一下作者先。

ajax开发中, 常遇到下面的几种情况:

1 服务端需要返回一段普通文本给客户端 
2 服务端需要返回一段HTML代码给客户端 
3 服务端需要返回一段XML代码给客户端 
4 服务端需要返回一段javascript代码给客户端 
5 服务端需要返回一段json串给客户端

对于每一种返回类型,规范的做法是要在服务端指定 response的contentType。
(当然 不指定绝大多数情况下也没什么问题 尤其是返回"非xml"的时候)

代码 
    普通文本 : text/plain
    HTML代码 : text/html
    XML代码 : text/xml
以上三个可以说是毫无争议的, 但是另外两种情况 就要注意一下了. 
javascript 的 contentType 按最标准的写法 应该是 application/javascript. 
而常用的 text/javascript 已经被 rfc定义为废弃的. 
但是,在这里暂时不建议使用 application/javascript
因为很多老旧浏览器并不支持 application/javascript 
而所有浏览器都支持 text/javascript. 
在标准和广泛的兼容性之间 还是暂且选择后者吧.

json 的 contentType 常见写法有: text/json & text/javascript . 
但是 这个 text/json 其实是根本不存在的, 而 text/javascript 在有些时候客户端处理起来会有歧义. 
对于json的contentType , rfc里定义的标准写法是 :application/json.

在这里毫无疑问 我们应该选择标准写法的 application/json

最后,附一下ContentType清单:

常用的:

[java] view plaincopy

  1. text/plain
  2. text/HTML
  3. text/xml
  4. test/javascript
  5. application/json
  6. image/GIF
  7. image/JPEG

清单:

[java] view plaincopy

  1. ".*"="application/octet-stream"
  2. ".001"="application/x-001"
  3. ".301"="application/x-301"
  4. ".323"="text/h323"
  5. ".906"="application/x-906"
  6. ".907"="drawing/907"
  7. ".a11"="application/x-a11"
  8. ".acp"="audio/x-mei-aac"
  9. ".ai"="application/postscript"
  10. ".aif"="audio/aiff"
  11. ".aifc"="audio/aiff"
  12. ".aiff"="audio/aiff"
  13. ".anv"="application/x-anv"
  14. ".asa"="text/asa"
  15. ".asf"="video/x-ms-asf"
  16. ".asp"="text/asp"
  17. ".asx"="video/x-ms-asf"
  18. ".au"="audio/basic"
  19. ".avi"="video/avi"
  20. ".awf"="application/vnd.adobe.workflow"
  21. ".biz"="text/xml"
  22. ".bmp"="application/x-bmp"
  23. ".bot"="application/x-bot"
  24. ".c4t"="application/x-c4t"
  25. ".c90"="application/x-c90"
  26. ".cal"="application/x-cals"
  27. ".cat"="application/vnd.ms-pki.seccat"
  28. ".cdf"="application/x-netcdf"
  29. ".cdr"="application/x-cdr"
  30. ".cel"="application/x-cel"
  31. ".cer"="application/x-x509-ca-cert"
  32. ".cg4"="application/x-g4"
  33. ".cgm"="application/x-cgm"
  34. ".cit"="application/x-cit"
  35. ".class"="java/*"
  36. ".cml"="text/xml"
  37. ".cmp"="application/x-cmp"
  38. ".cmx"="application/x-cmx"
  39. ".cot"="application/x-cot"
  40. ".crl"="application/pkix-crl"
  41. ".crt"="application/x-x509-ca-cert"
  42. ".csi"="application/x-csi"
  43. ".css"="text/css"
  44. ".cut"="application/x-cut"
  45. ".dbf"="application/x-dbf"
  46. ".dbm"="application/x-dbm"
  47. ".dbx"="application/x-dbx"
  48. ".dcd"="text/xml"
  49. ".dcx"="application/x-dcx"
  50. ".der"="application/x-x509-ca-cert"
  51. ".dgn"="application/x-dgn"
  52. ".dib"="application/x-dib"
  53. ".dll"="application/x-msdownload"
  54. ".doc"="application/msword"
  55. ".dot"="application/msword"
  56. ".drw"="application/x-drw"
  57. ".dtd"="text/xml"
  58. ".dwf"="Model/vnd.dwf"
  59. ".dwf"="application/x-dwf"
  60. ".dwg"="application/x-dwg"
  61. ".dxb"="application/x-dxb"
  62. ".dxf"="application/x-dxf"
  63. ".edn"="application/vnd.adobe.edn"
  64. ".emf"="application/x-emf"
  65. ".eml"="message/rfc822"
  66. ".ent"="text/xml"
  67. ".epi"="application/x-epi"
  68. ".eps"="application/x-ps"
  69. ".eps"="application/postscript"
  70. ".etd"="application/x-ebx"
  71. ".exe"="application/x-msdownload"
  72. ".fax"="image/fax"
  73. ".fdf"="application/vnd.fdf"
  74. ".fif"="application/fractals"
  75. ".fo"="text/xml"
  76. ".frm"="application/x-frm"
  77. ".g4"="application/x-g4"
  78. ".gbr"="application/x-gbr"
  79. ".gcd"="application/x-gcd"
  80. ".gif"="image/gif"
  81. ".gl2"="application/x-gl2"
  82. ".gp4"="application/x-gp4"
  83. ".hgl"="application/x-hgl"
  84. ".hmr"="application/x-hmr"
  85. ".hpg"="application/x-hpgl"
  86. ".hpl"="application/x-hpl"
  87. ".hqx"="application/mac-binhex40"
  88. ".hrf"="application/x-hrf"
  89. ".hta"="application/hta"
  90. ".htc"="text/x-component"
  91. ".htm"="text/html"
  92. ".html"="text/html"
  93. ".htt"="text/webviewhtml"
  94. ".htx"="text/html"
  95. ".icb"="application/x-icb"
  96. ".ico"="image/x-icon"
  97. ".ico"="application/x-ico"
  98. ".iff"="application/x-iff"
  99. ".ig4"="application/x-g4"
  100. ".igs"="application/x-igs"
  101. ".iii"="application/x-iphone"
  102. ".img"="application/x-img"
  103. ".ins"="application/x-internet-signup"
  104. ".isp"="application/x-internet-signup"
  105. ".IVF"="video/x-ivf"
  106. ".java"="java/*"
  107. ".jfif"="image/jpeg"
  108. ".jpe"="image/jpeg"
  109. ".jpe"="application/x-jpe"
  110. ".jpeg"="image/jpeg"
  111. ".jpg"="image/jpeg"
  112. ".jpg"="application/x-jpg"
  113. ".js"="application/x-javascript"
  114. ".jsp"="text/html"
  115. ".la1"="audio/x-liquid-file"
  116. ".lar"="application/x-laplayer-reg"
  117. ".latex"="application/x-latex"
  118. ".lavs"="audio/x-liquid-secure"
  119. ".lbm"="application/x-lbm"
  120. ".lmsff"="audio/x-la-lms"
  121. ".ls"="application/x-javascript"
  122. ".ltr"="application/x-ltr"
  123. ".m1v"="video/x-mpeg"
  124. ".m2v"="video/x-mpeg"
  125. ".m3u"="audio/mpegurl"
  126. ".m4e"="video/mpeg4"
  127. ".mac"="application/x-mac"
  128. ".man"="application/x-troff-man"
  129. ".math"="text/xml"
  130. ".mdb"="application/msaccess"
  131. ".mdb"="application/x-mdb"
  132. ".mfp"="application/x-shockwave-flash"
  133. ".mht"="message/rfc822"
  134. ".mhtml"="message/rfc822"
  135. ".mi"="application/x-mi"
  136. ".mid"="audio/mid"
  137. ".midi"="audio/mid"
  138. ".mil"="application/x-mil"
  139. ".mml"="text/xml"
  140. ".mnd"="audio/x-musicnet-download"
  141. ".mns"="audio/x-musicnet-stream"
  142. ".mocha"="application/x-javascript"
  143. ".movie"="video/x-sgi-movie"
  144. ".mp1"="audio/mp1"
  145. ".mp2"="audio/mp2"
  146. ".mp2v"="video/mpeg"
  147. ".mp3"="audio/mp3"
  148. ".mp4"="video/mpeg4"
  149. ".mpa"="video/x-mpg"
  150. ".mpd"="application/vnd.ms-project"
  151. ".mpe"="video/x-mpeg"
  152. ".mpeg"="video/mpg"
  153. ".mpg"="video/mpg"
  154. ".mpga"="audio/rn-mpeg"
  155. ".mpp"="application/vnd.ms-project"
  156. ".mps"="video/x-mpeg"
  157. ".mpt"="application/vnd.ms-project"
  158. ".mpv"="video/mpg"
  159. ".mpv2"="video/mpeg"
  160. ".mpw"="application/vnd.ms-project"
  161. ".mpx"="application/vnd.ms-project"
  162. ".mtx"="text/xml"
  163. ".mxp"="application/x-mmxp"
  164. ".net"="image/pnetvue"
  165. ".nrf"="application/x-nrf"
  166. ".nws"="message/rfc822"
  167. ".odc"="text/x-ms-odc"
  168. ".out"="application/x-out"
  169. ".p10"="application/pkcs10"
  170. ".p12"="application/x-pkcs12"
  171. ".p7b"="application/x-pkcs7-certificates"
  172. ".p7c"="application/pkcs7-mime"
  173. ".p7m"="application/pkcs7-mime"
  174. ".p7r"="application/x-pkcs7-certreqresp"
  175. ".p7s"="application/pkcs7-signature"
  176. ".pc5"="application/x-pc5"
  177. ".pci"="application/x-pci"
  178. ".pcl"="application/x-pcl"
  179. ".pcx"="application/x-pcx"
  180. ".pdf"="application/pdf"
  181. ".pdf"="application/pdf"
  182. ".pdx"="application/vnd.adobe.pdx"
  183. ".pfx"="application/x-pkcs12"
  184. ".pgl"="application/x-pgl"
  185. ".pic"="application/x-pic"
  186. ".pko"="application/vnd.ms-pki.pko"
  187. ".pl"="application/x-perl"
  188. ".plg"="text/html"
  189. ".pls"="audio/scpls"
  190. ".plt"="application/x-plt"
  191. ".png"="image/png"
  192. ".png"="application/x-png"
  193. ".pot"="application/vnd.ms-powerpoint"
  194. ".ppa"="application/vnd.ms-powerpoint"
  195. ".ppm"="application/x-ppm"
  196. ".pps"="application/vnd.ms-powerpoint"
  197. ".ppt"="application/vnd.ms-powerpoint"
  198. ".ppt"="application/x-ppt"
  199. ".pr"="application/x-pr"
  200. ".prf"="application/pics-rules"
  201. ".prn"="application/x-prn"
  202. ".prt"="application/x-prt"
  203. ".ps"="application/x-ps"
  204. ".ps"="application/postscript"
  205. ".ptn"="application/x-ptn"
  206. ".pwz"="application/vnd.ms-powerpoint"
  207. ".r3t"="text/vnd.rn-realtext3d"
  208. ".ra"="audio/vnd.rn-realaudio"
  209. ".ram"="audio/x-pn-realaudio"
  210. ".ras"="application/x-ras"
  211. ".rat"="application/rat-file"
  212. ".rdf"="text/xml"
  213. ".rec"="application/vnd.rn-recording"
  214. ".red"="application/x-red"
  215. ".rgb"="application/x-rgb"
  216. ".rjs"="application/vnd.rn-realsystem-rjs"
  217. ".rjt"="application/vnd.rn-realsystem-rjt"
  218. ".rlc"="application/x-rlc"
  219. ".rle"="application/x-rle"
  220. ".rm"="application/vnd.rn-realmedia"
  221. ".rmf"="application/vnd.adobe.rmf"
  222. ".rmi"="audio/mid"
  223. ".rmj"="application/vnd.rn-realsystem-rmj"
  224. ".rmm"="audio/x-pn-realaudio"
  225. ".rmp"="application/vnd.rn-rn_music_package"
  226. ".rms"="application/vnd.rn-realmedia-secure"
  227. ".rmvb"="application/vnd.rn-realmedia-vbr"
  228. ".rmx"="application/vnd.rn-realsystem-rmx"
  229. ".rnx"="application/vnd.rn-realplayer"
  230. ".rp"="image/vnd.rn-realpix"
  231. ".rpm"="audio/x-pn-realaudio-plugin"
  232. ".rsml"="application/vnd.rn-rsml"
  233. ".rt"="text/vnd.rn-realtext"
  234. ".rtf"="application/msword"
  235. ".rtf"="application/x-rtf"
  236. ".rv"="video/vnd.rn-realvideo"
  237. ".sam"="application/x-sam"
  238. ".sat"="application/x-sat"
  239. ".sdp"="application/sdp"
  240. ".sdw"="application/x-sdw"
  241. ".sit"="application/x-stuffit"
  242. ".slb"="application/x-slb"
  243. ".sld"="application/x-sld"
  244. ".slk"="drawing/x-slk"
  245. ".smi"="application/smil"
  246. ".smil"="application/smil"
  247. ".smk"="application/x-smk"
  248. ".snd"="audio/basic"
  249. ".sol"="text/plain"
  250. ".sor"="text/plain"
  251. ".spc"="application/x-pkcs7-certificates"
  252. ".spl"="application/futuresplash"
  253. ".spp"="text/xml"
  254. ".ssm"="application/streamingmedia"
  255. ".sst"="application/vnd.ms-pki.certstore"
  256. ".stl"="application/vnd.ms-pki.stl"
  257. ".stm"="text/html"
  258. ".sty"="application/x-sty"
  259. ".svg"="text/xml"
  260. ".swf"="application/x-shockwave-flash"
  261. ".tdf"="application/x-tdf"
  262. ".tg4"="application/x-tg4"
  263. ".tga"="application/x-tga"
  264. ".tif"="image/tiff"
  265. ".tif"="application/x-tif"
  266. ".tiff"="image/tiff"
  267. ".tld"="text/xml"
  268. ".top"="drawing/x-top"
  269. ".torrent"="application/x-bittorrent"
  270. ".tsd"="text/xml"
  271. ".txt"="text/plain"
  272. ".uin"="application/x-icq"
  273. ".uls"="text/iuls"
  274. ".vcf"="text/x-vcard"
  275. ".vda"="application/x-vda"
  276. ".vdx"="application/vnd.visio"
  277. ".vml"="text/xml"
  278. ".vpg"="application/x-vpeg005"
  279. ".vsd"="application/vnd.visio"
  280. ".vsd"="application/x-vsd"
  281. ".vss"="application/vnd.visio"
  282. ".vst"="application/vnd.visio"
  283. ".vst"="application/x-vst"
  284. ".vsw"="application/vnd.visio"
  285. ".vsx"="application/vnd.visio"
  286. ".vtx"="application/vnd.visio"
  287. ".vxml"="text/xml"
  288. ".wav"="audio/wav"
  289. ".wax"="audio/x-ms-wax"
  290. ".wb1"="application/x-wb1"
  291. ".wb2"="application/x-wb2"
  292. ".wb3"="application/x-wb3"
  293. ".wbmp"="image/vnd.wap.wbmp"
  294. ".wiz"="application/msword"
  295. ".wk3"="application/x-wk3"
  296. ".wk4"="application/x-wk4"
  297. ".wkq"="application/x-wkq"
  298. ".wks"="application/x-wks"
  299. ".wm"="video/x-ms-wm"
  300. ".wma"="audio/x-ms-wma"
  301. ".wmd"="application/x-ms-wmd"
  302. ".wmf"="application/x-wmf"
  303. ".wml"="text/vnd.wap.wml"
  304. ".wmv"="video/x-ms-wmv"
  305. ".wmx"="video/x-ms-wmx"
  306. ".wmz"="application/x-ms-wmz"
  307. ".wp6"="application/x-wp6"
  308. ".wpd"="application/x-wpd"
  309. ".wpg"="application/x-wpg"
  310. ".wpl"="application/vnd.ms-wpl"
  311. ".wq1"="application/x-wq1"
  312. ".wr1"="application/x-wr1"
  313. ".wri"="application/x-wri"
  314. ".wrk"="application/x-wrk"
  315. ".ws"="application/x-ws"
  316. ".ws2"="application/x-ws"
  317. ".wsc"="text/scriptlet"
  318. ".wsdl"="text/xml"
  319. ".wvx"="video/x-ms-wvx"
  320. ".xdp"="application/vnd.adobe.xdp"
  321. ".xdr"="text/xml"
  322. ".xfd"="application/vnd.adobe.xfd"
  323. ".xfdf"="application/vnd.adobe.xfdf"
  324. ".xhtml"="text/html"
  325. ".xls"="application/vnd.ms-excel"
  326. ".xls"="application/x-xls"
  327. ".xlw"="application/x-xlw"
  328. ".xml"="text/xml"
  329. ".xpl"="audio/scpls"
  330. ".xq"="text/xml"
  331. ".xql"="text/xml"
  332. ".xquery"="text/xml"
  333. ".xsd"="text/xml"
  334. ".xsl"="text/xml"
  335. ".xslt"="text/xml"
  336. ".xwd"="application/x-xwd"
  337. ".x_b"="application/x-x_b"
  338. ".x_t"="application/x-x_t"
时间: 2024-10-05 05:12:52

Ajax应用常见的HTTP ContentType设置的相关文章

ajax 的post方法 的content-type设置和express里应用body-parser

ajax的post方法相比get方法,在传参形式上很不一样, get把参数用'?'拼接在端口后,并且用'&'连接;而post则是需要在send参数里设置. 根据ajax实例xhr.setRequestHeader('content-type', )中第二个参数的不同,  send的参数也不相同. 最常用的有两种: application/x-www-form-encoded 和 application/json两种形式. 1 const username = document.getElemen

mysql中以blob形式存储的图片文件 通过ajax方式传输 在js中设置成img控件的src

第一步,读取blob数据, 第二步,将blob数据转换成byte数组 第三步,将byte数据进行base64加密转换成字符串并回传 第四步,接收字符串 第五步,将img控件的src设置成"data:image/jpeg;base64,"+接收的字符串; 相关代码: java: public String getAccountImg(String alias) { String sql = "SELECT imgrawdata FROM wx_account WHERE ali

HttpClient的Content-Type设置

HttpClient的Content-Type设置 最近在对接公司内容的一个云服务的时候,遇到一个问题,就是如果使用HttpClient如何设置post时候的Content-Type? public static string PostAdminSelect(string start) { string url = $"{BaseUrl}admin/select"; Model.AdminSelectQuery adminSelectQuery = new Model.AdminSel

contentType设置类型导致ajax post data 获取不到数据

ajax post  data  获取不到数据,注意 content-type的设置 .post/get关于 jQuery data 传递数据.网上各种获取不到数据,乱码之类的. 好吧今天我也遇到了,网上一查各种纠结.乱码不管先看获取不到数据. 因为之前一直用jQuery ajax get的方式传递参数, 默认没有设置过 contentType 的值 总结:get方式   这也就是我们说的get方式,参数是跟在url后边,与Content-Type无关. 1 var Skip = 49; //N

jquery ajax contentType设置

默认get方法没有contentType,post方法的contentType为:application/x-www-form-urlencoded; charset=UTF-8 (1) 设置成application/x-www-form-urlencoded, 后端可以直接用 String username = request.getParameter("username"); String password = request.getParameter("password

jQuery.ajax实现根据不同的Content-Type做出不同的响应

使用H5+ASP.NET General Handler开发项目,使用ajax进行前后端的通讯.有一个场景需求是根据服务器返回的不同数据类型,前端进行不同的响应,这里记录下如何使用$.ajax实现该需求. $.ajax({ 'url': 'GetWatermarkInfo.ashx', 'type':'post', 'data': { 'bgstyle': bgstyle, 'watermark': watermark }, success: function (data, status, xh

从content-type设置看Spring MVC处理header的一个坑

我们经常需要在HttpResponse中设置一些headers,我们使用Spring MVC框架的时候我们如何给Response设置Header呢? Sooooooooooooo easy, 看下面的代码: @RequestMapping(value = "/rulelist", method = RequestMethod.GET) @ResponseBody public String getRuleList(HttpServletRequest request, HttpServ

jquey中ajax请求的异步和同步设置

需要实现的效果:点击按钮后,分别调用三个接口更新数据,数据更新完之后刷新当前页面. (实现方式未必是最好的,此处只关注ajax的异步设置) 一般情况下使用 $.ajax() 是异步处理的,三个ajax请求分别发送后,可以在未有response返回的情况下继续执行下面reload()页面的代码 设置 async 为 false 则可以让ajax请求变成同步执行,就是执行完第一个请求后再执行第二请求. 1 function initData(nick, url, accessToken) { 2 $

ajax的常见几种写法以及用法

一.服务端数据格式 1.自定义po类 package com.hbut.ssm.po; /** * pojo类 * */ public class Children { private String name; private Integer age; private String gender; public Children(String name, Integer age, String gender) { super(); this.name = name; this.age = age