- URL: https://www.laruence.com/en/2011/01/20/1844.html
- Please include attribution when republishing.
I'd discussed this problem with someone before,,, today I'm home resting with a cold, so I recalled it and organized the notes as follows.
Many of our applications use Ajax, and in most cases it's a query-type operation. For example, submitting data — the Ajax just expects the server to return:
{status: 0, message:""} //status 0 means success; when non-zero, message contains the error info.
We know that HTTP status codes all in the 2xx range represent success, and HTTP's 204 (No Content) response means the operation succeeded but there's no data; the browser doesn't need to refresh the page or redirect to a new page.
The description of 204 in HTTP RFC 2616 is as follows:
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.
Similarly, there's 205 Reset Content, which means the operation succeeded and to reset the page (the 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.
So, when a service just returns success or not, you can try using the HTTP status code as the return information, and save on the extra data transfer — e.g. REST's DELETE and the query-style Ajax requests described above.
Finally, about 205: 205 means that after accepting the browser's POST request and processing it successfully, tell the browser that the execution succeeded, please clear the Form the user filled in, so the user can easily fill it in again.
In summary, 204 is suitable for updating the same Item many times, while 205 is suitable for submitting a series of Items multiple times.
But note: currently no browser supports 205. Most browsers treat 205 the same as 204 or 200.
Be First to Comment