brpaz
brpaz

Reputation: 3658

what should I return from server side code in an ajax call

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?

Upvotes: 0

Views: 183

Answers (3)

Greg
Greg

Reputation: 2493

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.

Upvotes: -1

Chris Diver
Chris Diver

Reputation: 19842

I would just return a 1 or a 0 given the boolean condition, minimize your overhead, then use JSON for more complex results.

Upvotes: 1

Ollie Edwards
Ollie Edwards

Reputation: 14749

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.

Upvotes: 2

Related Questions