Search code examples
phpjsonajaxrestcodeigniter

Should response text be set in the backend or front?


I'm building a site that uses quite a lot of AJAX requests to sign in, update information and add content.

I am currently using CodeIgnitor for the project and wondering whether a response of either success

"Success, you have updated your profile"

or error

"There was an error updating your profile!"

should have a message that is set in the JS function or if it should simply be put in the JSON response but unsure which is better for future development. What do you suggest?

The message is to be used in a toast/message notification on the page on request callback.

My original idea was to have a basic key value array somewhere in the project like so:

array(
    100 => "Cannot update profile",
    101 => "Successfully updated profile",
    102 => "Cannot authenticate user",
    103 => "Successfully authenticated!",
    104 => "Unable to load profile data"
)

But thought this might get confusing and sluggish over time.


Solution

  • I would say that it really depends on your application and the technologies you are using.

    In most cases I would go with the JSON response. Keep in mind that in the near future you will have to worry about translations, you may need to catch different exceptions for the user registration etc.

    I think it is better to handle this logic and have messages in backend.