What is better to return to ajax: html data or json? I have in my php(mvc) controller PostController
where i have metod index
. That method return array with all users posts,comments, post likes, comment likes, etc. It is a large merged array! Somthing like on facebook wall!
All that i render in view/home/index.php
file with php
and with jquery ajax
i load
that file in specific div
But looking fb and other social network sites they return json object.
In my case if i return json_encode
json to ajax i must loop with each
all posts and commets.
So if i loop with $.each
i must put to much html codes inside $.each
. It is not ugly and bad practice to put all that html code inside javascript looping? Or exist better solution for this loop?
It depends on what you want to do with the data. If you use the data as a presentation of data on which you will not perform any calculation, you might use HTML. That way you can easily replace a part of your website with javascript, without having to deal with structuring your data.
However, If you want to perform calculations on the data or if you want to use only parts of the data on different places, JSON is the way to go.
Please note that this is just my opinion.
hey you can define what type of data do you want like below
Datatype:json or datatype:html in jquery ajax