HTTP请求返回码204

[| 不指定 2011/10/27 18:49]
| |
    今天测试lighttpd是否支持delete请求,发现webdav模块可以实现此功能。不过发现http返回码是204,查了一下,原来此状态码的意思是说请求成功了,但是没有结果返回来。搜到鸟哥一篇文章,讲的很不错,转载一下:


http://www.laruence.com/2011/01/20/1844.html

之前和人讨论过这个问题,,, 今天感冒在家休息, 就回忆了一下, 整理如下.

我们很多的应用在使用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同样对待.
Tags: , , , ,
by snooda | 分类: 默认分类 | 评论(9) | 引用(0) | 阅读(63846)
111
2019/03/15 16:02
fearshockpuke
1
2019/02/28 19:45
gringringringringringringringringringrin
咖啡 Homepage
2017/08/18 15:33
速溶咖啡 http://www.coffeedj.net
aaa Email Homepage
2017/04/14 17:02
killkillkill
ww2e3er
2017/03/31 15:21
好的,写的不错
4324324 Email Homepage
2017/03/30 11:42
32422243killkillkillkillkillkillkillkillkillkillkillkillkillkillkillkillkill
马云
2016/09/03 10:11
zanstupidstupidzan
ff
2016/02/16 17:08
angerfearfearuplook
引用
文字
文字文字killkillkillkillkillkillkillkillkillkillkillkillkillkillkillkillkillkillkill
asfd Email Homepage
2015/06/17 17:59
killkillkillkillkillkillkillkillkillkillkillkillkillkillkillkill
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]