views:

41

answers:

2

Here's the situation:

I've got two partial views. One has a form.

What needs to happen is when the form is posted via an AJAX request, if the operation succeeds, the area with the second partial is repopulated with the content. However, if the posted data was invalid, the original partial is repopulated with an error message.

I'm using jQuery and the ajaxForm plugin to handle the form posts and responses. Would it be "wrong"/bad coding/wtf-worthy to conditionally use $.html() to replace the content in one area when it's a 200, and a different area if it's a 500?

To me, this idea smells, but I'm not sure how else to accomplish the goal.

+3  A: 

Sounds like you should use 400 (Bad Request), not 500 (Internal Server Error), since the error is on the client side.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Peter Stone
A valid point! Doesn't change the smelliness though.
Daniel Schaffer
It sounds like your objection is to using an error code when the app hasn't crashed - is that right? If so, think about the other 4XX codes - 404 Not Found, 401 Auth Required etc.The only reason I might avoid using an error code in this situation is if it causes odd behavior on the part of a user-agent. But I don't think that should be an issue in this case.
Peter Stone
+1  A: 

Yeah, I wouldn't think that's a good idea. I don't think you should use either 400 or 500, but always 20x.

In this case, you could play with content-types, and have your error messages returned as JSON strings instead of HTML. I have used this kind of idea before, although using prototypejs instead of jQuery and it worked well.

wtaniguchi