PbootCms自帶了API接口,我們可以通過(guò)這個(gè)API接口http://域名/api.php/list/ 調(diào)取分頁(yè)所需的數(shù)據(jù)。
我建議這種分頁(yè),不要一上來(lái)就全部使用Ajax獲取數(shù)據(jù),因?yàn)锳jax是不會(huì)被百度收錄的。
我建議的做法是,在列表頁(yè)先使用模板標(biāo)簽輸出一些數(shù)據(jù)。例如:
<div class="newslist"> {pboot:list num=6} <li> <a href="[list:link]">[list:title]</a> </li> {/pboot:list} <a href="javascript:;" class="loadmore">加載更多</a> </div>
先讀取6條數(shù)據(jù),讓搜索引擎醉少能抓取到醉新的6條文章。
然后再使用Ajax去調(diào)取剩余的新聞內(nèi)容,實(shí)現(xiàn)點(diǎn)擊加載更多的效果。
下面就是核心AJAX調(diào)取API數(shù)據(jù)部分代碼
<script> $(function(){ var Page = 1; // 每頁(yè)展示12個(gè) var Num = 6; $(document).on('click','.loadmore',function(){ // 頁(yè)數(shù) Page++; $.ajax({ type: 'POST', url: '/api.php/list/{sort:scode}/page/' + Page + '/num/' + Num + '/order/sorting', dataType: 'json', data: { appid: '{pboot:appid}', timestamp: '{pboot:timestamp}', signature: '{pboot:signature}', }, success: function( response, status ){ console.log(response); var Data = response.data; if( response.code ){ //獲取數(shù)據(jù)成功 var Html = ''; jQuery.each( Data, function( index, value ){ //構(gòu)建HTML Html += '<li>'; Html += ' <a href="'+ value.contentlink +'">'+ value.title +'</a>'; Html += '</li>'; }); // 為了測(cè)試,延遲1秒加載 setTimeout(function(){ // 插入數(shù)據(jù)到頁(yè)面,放到醉后面 $('.newslist ul').append(Html); },500); }else{ $('.loadmore').slideUp(); } }, error: function(xhr, type){ console.log('Ajax error!'); } }); }); }); </script>
簡(jiǎn)單解釋一下這個(gè)代碼,從第二頁(yè)開(kāi)始讀取,每次讀取6條。為什么從第二頁(yè)開(kāi)始讀取?因?yàn)榍懊嫖覀円呀?jīng)在模板里輸出6條數(shù)據(jù)啦。
請(qǐng)注意在后臺(tái)開(kāi)啟WebAPI,如下圖。
其中API強(qiáng)制認(rèn)證,可以按你的實(shí)際情況選擇啟用。
到此教程結(jié)束。
版權(quán)聲明:本文除特別說(shuō)明外均由博主原創(chuàng),尊重共享,歡迎轉(zhuǎn)載!轉(zhuǎn)載請(qǐng)注明來(lái)源。https://www.phpjin.com/cms/41.html
有關(guān)我們服務(wù)的更多信息,請(qǐng)聯(lián)系項(xiàng)目經(jīng)理
15899750475 楊先生