Press "Enter" to skip to content

HTTP 204和205的应用

之前和人讨论过这个问题,,, 今天感冒在家休息, 就回忆了一下, 整理如下.
我们很多的应用在使用Ajax的时候, 大多数情况都是询问型操作, 比如提交数据, 则Ajax只是期待服务器返回:

{status: 0, message:""} //status 0代表成功, 非零的时候, message中包含出错信息.

我们知道HTTP的状态码, 2xx都是表示成功, 而HTTP的204(No Content)响应, 就表示执行成功, 但是没有数据, 浏览器不用刷新页面.也不用导向新的页面.
在HTTP RFC 2616中关于204的描述如下:

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent’s active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent’s active view.

类似的还有205 Reset Content, 表示执行成功, 重置页面(Form表单).

The server has fulfilled the request and the user agent SHOULD reset the document view which caused the request to be sent. This response is primarily intended to allow input for actions to take place via user input, followed by a clearing of the form in which the input is given so that the user can easily initiate another input action.

于是, 当有一些服务, 只是返回成功与否的时候, 可以尝试使用HTTP的状态码来作为返回信息, 而省掉多余的数据传输, 比如REST中的DELETE和如上所述的查询式Ajax请求.
最后说说205, 205的意思是在接受了浏览器POST请求以后处理成功以后, 告诉浏览器, 执行成功了, 请清空用户填写的Form表单, 方便用户再次填写,
总的来说, 204适合多次对一个Item进行更新, 而205则适合多次提交一个系列的Item.
但, 请注意, 目前还没有一个浏览器支持205, 大部分的浏览器, 都会把205当做204或者200同样对待.

47 Comments

  1. Prestige park grove
    Prestige park grove July 7, 2023

    We are very grateful that you shared this knowledge with us all.

  2. reference
    reference November 8, 2019

    reference
    HTTP 204和205的应用 | 风雪之隅

  3. Zbea168.com
    Zbea168.com August 25, 2019

    Zbea168.com
    HTTP 204和205的应用 | 风雪之隅

  4. Fortnite Wallhack
    Fortnite Wallhack August 15, 2019

    Fortnite Wallhack
    HTTP 204和205的应用 | 风雪之隅

  5. CommandNotFound
    CommandNotFound January 10, 2019

    之前204用的多一些,205还是头一次。学习了

  6. Blaine
    Blaine June 11, 2016

    In addition to garage door restore and installations, we are able
    to also do installations and repairs for gates and fences.

  7. Project Supremacy
    Project Supremacy June 8, 2016

    Helpful information. Lucky me I discovered your website by chance,
    and I am surprised why this twist of fate did not happened earlier!
    I bookmarked it.

  8. Raphael
    Raphael June 1, 2016

    If some one wishes to be updated with most up-to-date technologies therefore he must be pay a visit this site
    and be up to date daily.

  9. welcomeMK
    welcomeMK May 17, 2016

    这个可以试试

  10. free surf
    free surf April 8, 2016

    Good response in return of this difficulty with solid arguments
    and describing the whole thing about that.

  11. Leo
    Leo March 16, 2016

    Fantastic website you have here but I was wanting to know if
    you knew of any community forums that cover the same topics talked about
    in this article? I’d really love to be a part of community
    where I can get feedback from other knowledgeable individuals
    that share the same interest. If you have any recommendations, please let me know.
    Thanks a lot!

  12. loretta
    loretta February 12, 2016

    I’ll immediately grasp your rss as I can’t find your email subscription link or e-newsletter service.
    Do you’ve any? Kindly permit me know in order that I could subscribe.
    Thanks.

  13. Sports98
    Sports98 February 4, 2016

    对于各种浏览器的水土不福。个人觉得这个状态码在专有应用使用还是不错的。如果作为一个公众应用服务可控性太差..

  14. coach
    coach December 12, 2015

    One of my favorite methods of keeping focused on a goal is to constantly ask myself, “Is what I’m doing right now bringing me closer to my goal or further away?” Once you answer that question, you’ll know what to do.
    coach http://coachoutletonline.tote2015.com/

  15. bouwtechnische keuring
    bouwtechnische keuring November 30, 2015

    It is in reality a great and helpful piece of info. I’m satisfied
    that you simply shared this useful information with us. Please keep us
    up to date like this. Thank you for sharing.

  16. 杰夫
    杰夫 June 25, 2015

    加个 data 放置返回数据就更好了

  17. Jaunita
    Jaunita March 23, 2015

    Aw, this was an extremely nice post. Finding the time and actual effort to
    make a superb article… but what can I say… I hesitate a lot and never manage to get nearly
    anything done.
    Feel free to surf to my web site – credit
    sans justificatif a fournir (Jaunita)

  18. free rp codes
    free rp codes March 9, 2015

    Fantastic blog you have here butt I was wondering if yyou knew of
    any community orums that cover the same topics
    talked about in this article? I’d really love to be a part of community were I can get feed-back
    from other knowledgeable individuals that share the same interest.
    If you have any recommendations, please let me know.
    Cheers!

  19. Anonymous
    Anonymous February 23, 2015

    Actually no matter if someone doesn’t be aware of after that its
    up to other visitors that they will help, so here it occurs.

  20. safestep walk in tub cost
    safestep walk in tub cost January 9, 2015

    I believe what you said was actually very reasonable.
    However, consider this, suppose you were to create a killer post title?
    I mean, I don’t want to tell you how to run your website,
    but suppose you added a headline that grabbed folk’s attention? I mean HTTP 204和205的应用 | 风雪之隅 is kinda plain.
    You ought to look at Yahoo’s home page and see how they create post headlines to grab viewers interested.
    You might try adding a video or a pic or two
    to get people interested about what you’ve written.
    In my opinion, it might bring your posts a little
    bit more interesting.

  21. careerrocketeer.com
    careerrocketeer.com November 18, 2014

    great put up, very informative. I ponder why the opposite experts of this
    sector don’t realize this. You must continue your writing.
    I’m confident, you’ve a great readers’ base already!

  22. Beyonce Liposuction
    Beyonce Liposuction November 13, 2014

    Hello, i think that i noticed you visited my site so i came to return the favor?.I’m trying
    to to find issues to improve my website!I suppose its adequate
    to use some of your concepts!!

  23. phlebitis treatment
    phlebitis treatment October 26, 2014

    Eliminate Dimply skin Thru Natural Massage
    therapies
    There are only a number of women who do not have problems with dimply skin and for that reason there are various “magic supplements” available that aid in reducing your
    bumpy skin. Still each and every girl who has experimented with it has learned that these kinds of anti-cellulite products
    have little or no effect. The most effective way to remove your bumpy
    skin may be the oldest method inside the book!
    Using a targeted anti-cellulite massage therapy, it is possible to successfully remove cellulite from the thighs, legs and bottom.

  24. Alice
    Alice October 22, 2014

    You should bbe a part of a contest for one oof the most useful blogs on the internet.
    I’m going to recommend this website!

  25. download tool
    download tool October 17, 2014

    that you are super LUCKY annd (2) total aand complete BELIEF.
    I highly recommend downloading Spybot Search & Destroy, a
    free program, to locate spyware and Lavasoft’s Adaware, also a free program,
    to catch whatever might bbe missed by Spybot. 10meanwhile offers the same image downloading features off the Batch Images Downloader extension ‘ and more.

  26. reklama
    reklama October 16, 2014

    This blog was… how do you say it? Relevant!! Finally I have found something
    which helped me. Kudos!

  27. Reginald
    Reginald October 13, 2014

    The Phone Broadcast club also uses special software
    to scrub against people that are on the “DNC” or Do Not
    Call List. Whether we’re learning from Robert Kiyosaki, Robert Allen, the folks at Power Prospecting System,
    or our upline mentors, we are always learning and pioneering marketing techniques.
    Many problems are common among multi-level marketers, such as struggling to generate enough leads,
    or make a reasonable amount of sales.

  28. vintage rings
    vintage rings October 5, 2014

    Greetings! Very helpful advice iin this particular post!
    It’s the little changes that will make the
    largest changes. Thahks a lot for sharing!}

  29. 怪奇工厂
    怪奇工厂 November 24, 2013

    受益颇丰,谢谢鸟哥

  30. […] 来源:http://www.laruence.com/2011/01/20/1844.html This entry was written by happy , posted on Tuesday April 12 2011at 08:04 pm , filed under Front-End Development, program . Bookmark the permalink . Post a comment below or leave a trackback: Trackback URL. Leave a Reply Click here to cancel reply. […]

  31. wclssdn
    wclssdn April 11, 2011

    不错.. 是不是也可以用其他的码呢? 比如.. 失败了.. 我给他个500- -!! 有空试试~~~

  32. 贝壳里的海
    贝壳里的海 March 19, 2011

    平时接触这两个状态不多,不是很懂

  33. iterse's blog
    iterse's blog January 25, 2011

    比较纠结!
    能帮我加个友链吗,谢谢!

  34. cfc4n
    cfc4n January 24, 2011

    用firebug调试ajax的时候,经常返回200,但firebug里看到的却是“加载源码失败”,不知道是不是跟这个有关。

  35. weskerjax
    weskerjax January 20, 2011

    400,401 也可以用來處理錯誤狀況
    並可以用 responseText 來傳送錯誤訊息
    對於成功訊息的處理我倒是沒有關注到
    感謝你的文章

  36. xiaobai
    xiaobai January 20, 2011

    看到最后一句话,咋这么纠结呢……

Comments are closed.