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?
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.
I would just return a 1 or a 0 given the boolean condition, minimize your overhead, then use JSON for more complex results.
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.
精彩评论