douhu2525 2015-12-10 10:50
浏览 14
已采纳

如何通过Ajax调用处理大量HTML [关闭]

I was wondering if what I'm doing is fine, it works but I've this obsession to find a possible better way to do it. Basically I've to serve a user profile in a "Modal-box". So a user clicks on a button/link and this window is dynamically generated and its content-div is populated with the result of an AJAX request. Actually the AJAX request calls a php script that get the data from a MySQL database, get an HTML template through the php output-buffer and it stores that file as a string in a variable, then this variable is stored in an array, json-encoded and echoed out as something like that:

[ { success: 1, html: "HTML already formatted with data from the PHP script" } ]

On the client side, when my AJAX call is done I just fill my modal-content div with the html, so it is something like that:

.done( function ( srv-data ) {

   if ( srv-data.success === 1 ){

      $( "#modal-content" ).html( srv-data.html );

   }

});

Is this a reasonable way to serve my HTML to the front-end? Or there are better ways to do something similar? I use this solution just for that case where I've a lot of HTML to render with a lot of fields in the database to work with, other AJAX responses are just looped through the Object and rendered on the client side.

Thanks for your time.

  • 写回答

2条回答 默认 最新

  • dongma7796 2015-12-10 11:32
    关注

    I don't know if this is a better way of doing things, but here is what I generally do when I have a lot of data to be passed to a modal.

    I send the response as a JSON from the server.

    So the AJAX request will result in something like this

    {
        amount: 100,
        approved: 0,
        approved_at: "0000-00-00 00:00:00",
        approved_by: "",
        created_at: "2015-12-05 16:24:33",
        id: 2,
        overhead: "dummy data",
        remarks: "Head Approved",
        request_id: 1500002,
        type: "advance",
        updated_at: "2015-12-05 16:29:48",
    }
    

    Since I already have a structure for the Modal, I can use JQuery to insert the values into the modal.

    .done(function(data){
    var json = jQuery.parseJSON(
    $('#proj_id').html(json.proj_id);
    $('#overhead').html(json.overhead);
    $('#type').html(json.type);
    $('#amount').html(json.amount);
    $('#projectModal').modal('show');
    }
    

    In this method, we can send only the data that needs to be sent from the server and the processing happens at the client side. So, the network overhead is minimal. Also, if we are serving JSON, then any kind of client can use the data (Android/Web)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改