weixin_33722405 2016-04-25 07:55 采纳率: 0%
浏览 13

PHP Ajax返回json或html

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?

  • 写回答

2条回答 默认 最新

  • weixin_33720186 2016-04-25 08:07
    关注

    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.

    评论

报告相同问题?