Hi guys. Lets say i have a ajax method which call a script that checks if a user exists in the database. What is best to be returned from the server side code?. Should I Just make an echo "notfound" and then compare the response in the javascript, return a json object or any other suggestion?
+2
A:
I'd return a json object. Echoing a string or a boolean will work, but it's best to stick to established convention so that when you add other more complex AJAX calls the return format is consistent.
Ollie Edwards
2010-08-02 11:34:47
I always create a Response base type that all the response types inherit. So, for a `GetUsers` method, I would create a `GetUsersResponse` that inherits from `ServiceResponse` type. That way, I can common out properties like `Success` (`Boolean`) and `Message` (`String`).
decyclone
2010-08-02 11:50:55
+1
A:
I would just return a 1 or a 0 given the boolean condition, minimize your overhead, then use JSON for more complex results.
Chris Diver
2010-08-02 11:36:17
A:
You can also return status in the headers, for example all error messages could be returned with status 500 and all success with 200. That would give you straight indication that there has been unsuccessful attempt of your action, for example failed login, no user found etc.
Greg
2010-08-02 11:38:48
It's not a good idea to return a 500 for a non-error condition (ie no user was found but the REQUEST was fine) Status codes should only refer to the status of the request, not the operation they invoke.
Ollie Edwards
2010-08-02 12:50:13