dongqiaogouk86049
2014-10-09 10:01
浏览 64

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.

    点赞 打赏 评论

相关推荐 更多相似问题