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同样对待.

36 Comments

  1. Blaine
    Blaine 2016-06-11

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

  2. Project Supremacy
    Project Supremacy 2016-06-08

    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.

  3. Raphael
    Raphael 2016-06-01

    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.

  4. welcomeMK
    welcomeMK 2016-05-17

    这个可以试试

  5. free surf
    free surf 2016-04-08

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

  6. Leo
    Leo 2016-03-16

    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!

  7. loretta
    loretta 2016-02-12

    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.

  8. Sports98
    Sports98 2016-02-04

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

  9. coach
    coach 2015-12-12

    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/

  10. 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.

  11. 杰夫
    杰夫 2015-06-25

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

  12. Jaunita
    Jaunita 2015-03-23

    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)

  13. free rp codes
    free rp codes 2015-03-09

    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!

  14. Anonymous
    Anonymous 2015-02-23

    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.

  15. 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.

  16. careerrocketeer.com
    careerrocketeer.com 2014-11-18

    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!

  17. Beyonce Liposuction
    Beyonce Liposuction 2014-11-13

    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!!

  18. phlebitis treatment
    phlebitis treatment 2014-10-26

    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.

  19. Alice
    Alice 2014-10-22

    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!

  20. download tool
    download tool 2014-10-17

    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.

  21. reklama
    reklama 2014-10-16

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

  22. Reginald
    Reginald 2014-10-13

    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.

  23. vintage rings
    vintage rings 2014-10-05

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

  24. […] 来源: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. […]

  25. wclssdn
    wclssdn 2011-04-11

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

  26. cfc4n
    cfc4n 2011-01-24

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

  27. weskerjax
    weskerjax 2011-01-20

    400,401 也可以用來處理錯誤狀況
    並可以用 responseText 來傳送錯誤訊息

    對於成功訊息的處理我倒是沒有關注到
    感謝你的文章

  28. xiaobai
    xiaobai 2011-01-20

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

Leave a Reply

Your email address will not be published. Required fields are marked *