dongqiaogouk86049 2014-10-09 10:01
浏览 67
已采纳

ajax加载内容渲染在php返回

I'm using ajax call api return json and render html append to page like below.
I have question is is possible after post parameter to api function then api function render in php then return rendered data to js, js just only append?

Because there is to much html structure if i write in js so difficult to read. Wondering most people how to do this?

$.ajax({
    url: public_path+'/api_for_ajax/category/'+category_id+'/'+visible+'/'+rows_perpage+'/'+page,
    type: 'POST',
    processData: false,
    contentType: false,
    async: false
}).done(function(response) {
    var response = JSON.parse(response);
    $(data_article.article).each(function(i, each_data_article) {
        var output = '<div class="article-list"><a href="'+public_path+'/article/'+each_data_article.id+'">
        <div class="thumbnail"><img src="'+public_path+'/assets/thumbnails/'+each_data_article.thumbnail.thumbnail_id+'/'+each_data_article.thumbnail.file_name+'.'+each_data_article.thumbnail.file_format+'" alt=""></div>
        <div class="subject">'+each_data_article.subject+'</div>
        </a></div>';
       // append
    });
});

api

$data_select_category_id_page = $this->article_model->select_category_id_page($response_message, $category_id, $visible, $rows_perpage, $page);

$data = array();
$data['article'] = $data_select_category_id_page;

echo json_encode($data);

I tried in slim framework, it not work why??

api

$data_select_category_id_page = $this->article_model->select_category_id_page($response_message, $category_id, $visible, $rows_perpage, $page);

// 1
$app->render('frontstage/article/page_view.php', array(
  'data_select_category_id_page' => $data_select_category_id_page,
  )
);


// 2
return <<<HTML
<div><?php print_r($data_select_category_id_page);?></div>
HTML;

layout

<?php $column_count = 1; ?>
<div class="row">
    <?php foreach ($data['article']['article'] as $i => $each_article) { ?>
        <?php if ($i >= 4) { ?>
    <div class="article-list">
        <a href="<?php echo $uri['public']; ?>/article/<?php echo $each_article['id']; ?>">
            <div class="thumbnail"><img src="<?php echo $uri['public']; ?>/assets/thumbnails/<?php echo $each_article['thumbnail']['thumbnail_id']?>/<?php echo $each_article['thumbnail']['file_name']?>.<?php echo $each_article['thumbnail']['file_format']?>" alt=""></div>
            <div class="subject"><?php echo $each_article['subject'];?></div>

            <div class="category-list-container">
            <?php foreach ($each_article['category'] as $each_article_category) { ?>
                <div class="category-list"><?php echo $each_article_category['subject']; ?></div>
            <?php } ?>
            <div class="clear"></div>
        </div>
        <?php 
            $old_date = $each_article['create_at'];
            $old_date_timestamp = strtotime($old_date);
            $new_date = date('d M Y', $old_date_timestamp);
        ?>
        <div class="create-at"><?php echo $new_date;?></div>
    </a>
</div>
<?php if (($column_count % 4) == 0) { ?>
<div class="clear"></div></div><div class="row">
<?php } ?>
<?php $column_count++;?>
<?php } ?>
<?php } ?>
<div class="clear"></div>
  • 写回答

1条回答 默认 最新

  • dqudtskm49788 2014-10-09 10:07
    关注

    It is possible to have the server return the html rendered rather than JSON.

    $.post( "ajax/test.php", function( data ) {
      $( ".result" ).html( data );
    });
    

    Where test.php returns some kind of HTML data.

    The test.php code will contain the logic to render the relevant html. Using the OP example as the logic, would look something like (untested) :

    $public_path = '/sitepath/';
    foreach($data_select_category_id_page as $article)
    {
       echo '<div class="aricle-list"><a href="' . $public_path . '/article/' . $article->id . '">
        <div class="thumbnail"><img src="' . $public_path . '/assets/thumbnails/' . $article->thumbnail_id . '/' . $article->thumbnail->file_name . '.' . $article->thumbnail->file_format . '" alt=""></div>
        <div class="subject">' . $article->subject . '</div>
        </a></div>';
    }
    

    Or alternatively use a layout to render the html.

    Beaware that the size of the rendered html being returned from the server is large than JSON.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题