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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据