drui0508 2013-07-03 23:43
浏览 23

格式化HTML服务器端还是客户端? [关闭]

I have a PHP page that retrieves a list from the DB, and I want to display it on a loaded page using Ajax.

Should I format it on the PHP side (HTML formatting), and just retrieve the data, or pass it to JS like dataA:dataB:dataC and format it client-side?

there won't be a lot of people using it, but I would like to know which is better (if there is a better method without taking the amount of users into account)

  • 写回答

1条回答 默认 最新

  • dongzhuanlei0768 2013-07-03 23:46
    关注

    Both will work fine. However in my opinion if you're gonna use ajax - and transfer information - a better practice will be to wrap the data in JSON format and parse it on the client's machine.

    Example of the php output:

    {
       "row1":{"field1":"value11", "field2":"value12"}
       "row2":{"field2":"value21", "field2":"value22"}
       ...
    }
    

    Exmaple of parsing:

    $.ajax(...).done(function(result){
        $.each(result, function(index,value){
            $('#conatiner').append('<div>'+index+': field1='+value.field1+', field2='+value.field2+'</div>')
        })
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?