Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider using HTTP status codes in responses for XMLHttpRequests #22

Open
GoogleCodeExporter opened this issue Mar 25, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 10 Feb 2009 at 10:01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant