Ajax应用常见的HTTP ContentType设置

前端之家收集整理的这篇文章主要介绍了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 plain copy
  1. text/plain
  2. text/HTML
  3. text/xml
  4. test/javascript
  5. application/json
  6. image/GIF
  7. image/JPEG

清单:

copy

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

猜你在找的Ajax相关文章