Getting HTML or JSON response from ajax call, Which is good?
ajax, html, javascript, jquery, json
Solution
To make a service (server-side script) the most re-usable or even make it into an `API` - the suggested way is to return `JSON` data (converted from data models) to the front end, where using JavaScript you can populate the data to the `HTML`.
As for the `HTML` - you can certainly make the server return the response as `HTML` (setting the correct mime & content type in headers) but this gives the server control over the UI layer and the separation between the interface and the server/db is not balanced properly...
Problem
I have a functionality in my project where I have implemented the search functionality. On submitting the form via ajax, I need to show all the results in a division. There are two ways I can do this. - Getting the JSON data as ajax response and bind it to the HTML elements. - I can also get the completely formatted HTML response from the ajax call and can directly bind it to the result div on the search page. So which way is suggestible ?