cleverking / django-voting

Automatically exported from code.google.com/p/django-voting
Other
0 stars 0 forks source link

Consider using HTTP status codes in responses for XMLHttpRequests #22

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The API for XMLHttpRequests to the voting app returns a JSON response with
a boolean "success" property and a description of the error.

A superior approach might be to embrace http status codes and use different
http responses to represent different error types.

For example (I'm referencing Django's HttpRequest subclasses below):

HttpResponseForbidden() could be used in place of json_error_response('Not
authenticated.') when a user is not authenticated.

HttpResponseNotAllowed() could be used in place of
json_error_response('XMLHttpRequest votes can only be made using POST.')

HttpResponseNotFound() could be used when the object does not exists.

etc.

The advantage of this approach is that it obviates the need for a "success"
property (you can just pass single text string or a JSON object if more
structure is required). It's also very easy to handle these responses in
frameworks like jQuery, which as an example, fires an error() callback when
a status code like 403 or 404 is received but a success() call back on 200.

If you're interested I could submit a patch.

(LOVE django-voting btw, implementing it for an application i'm developing
right now.)

Original issue reported on code.google.com by jim.dalt...@gtempaccount.com on 10 Feb 2009 at 10:01