Home >Backend Development >PHP Tutorial >文章列表底部点击加载更多,即使分享代码也是提问题

文章列表底部点击加载更多,即使分享代码也是提问题

WBOY
WBOYOriginal
2016-06-23 13:26:44766browse

JQ ajax代码:

<script type="text/javascript">	//格式化时间戳	function getLocalTime(nS) {		return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');	}	var page = 3;	$(function() {		$('#morse').on('click', function() {			page += 3;			$.ajax({	            url:"__URL__/index",	            data:{					p:page,				},	            type:"post",	            success:function(data){					var html = '';					for(var i in data){						var addtime	= getLocalTime(data[i].addtime);						html += '<div class=\"am-container\" id=\"morelist\">';							html +='<ul id=\"list\" class=\"am-list\">';							  	html +='<li class=\"am-g am-list-item-desced am-padding-horizontal-sm\">';							  		html +='<a class=\"am-list-item-hd\" target=\"_blank\" href=\"\">'+data[i].title+'</a>';							  		html +='<div class=\"am-list-item-text\">发布时间:'+addtime+'</div>';							  		html +='<div class=\"am-list-item-text\">';							  		html +='总阅读量:<span class=\"am-text-danger\">'+data[i].readcount+'</span> ';							  	html +='</li>';							html +='</ul>';						html +='</div>';					}					$("#list").append(html);//在后面追加显示				}			});       	});	});</script>

ThinkPHP3.2.3前台控制器index方法:
public function index(){		if(!empty($_POST['p'])){//点击加载更多 			$p = $_POST['p'];//3 6 9			$amount = 3; //显示条数			//$count = M("micro_article")->count();//总数			$Model = new \Think\Model();			$list  = $Model->query("SELECT * FROM `day_micro_article` ORDER BY addtime DESC LIMIT $p,$amount");			$this->ajaxReturn($list);		}		$count = M("micro_article")->count();        $Page  = new \Think\Page($count, 3);		        $show  = $Page->show();		$Model = new \Think\Model();		//默认显示最新三条	    $list  = $Model->query("SELECT * FROM `day_micro_article` WHERE `status` = 1 AND `is_rec` = 1 ORDER BY `addtime` DESC LIMIT " . $Page->firstRow . ',' . $Page->listRows);		$shouyi= M("Micro_customlucre")->where(array("zid"=>1))->find();		$this->assign('list', $list);		$this->assign('vo1', $shouyi);		$this->assign('show', $show);		$this->display();	}


我的问题:getLocalTime格式化时间戳函数,拿出来的时间2015/9/4 下午7:07 这样的格式的;
如图:

我想要 2015/9/4 19:07:22这样的格式



回复讨论(解决方案)

function getLocalTime(nS) {  var d = new Date(parseInt(nS) * 1000);  return d.getYear() + '/' + (d.getMonth()+1) + '/' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();}


你的 $.ajax 缺少 dataType:'json' 属性,所以返回的 data 是字符串。
作为分享这是不道德的(有意隐藏关键代码)

你返回的数据是txt,不是json。所以解释不出。

<script type="text/javascript">    //格式化时间戳    function getLocalTime(nS) {        return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');    }    var page = 3;    $(function() {        $('#morse').on('click', function() {            page += 3;            $.ajax({                url:"__URL__/index",                data:{                    p:page,                },                type:"post",               dataType:"JSON",                success:function(data){                    var html = '';                    for(var i in data){                        var addtime    = getLocalTime(data[i].addtime);                        html += '<div class=\"am-container\" id=\"morelist\">';                            html +='<ul id=\"list\" class=\"am-list\">';                                  html +='<li class=\"am-g am-list-item-desced am-padding-horizontal-sm\">';                                      html +='<a class=\"am-list-item-hd\" target=\"_blank\" href=\"\">'+data[i].title+'</a>';                                      html +='<div class=\"am-list-item-text\">发布时间:'+addtime+'</div>';                                      html +='<div class=\"am-list-item-text\">';                                      html +='总阅读量:<span class=\"am-text-danger\">'+data[i].readcount+'</span> ';                                  html +='</li>';                            html +='</ul>';                        html +='</div>';                    }                    $("#list").append(html);//在后面追加显示                }            });           });    });</script>

function getLocalTime(nS) {  var d = new Date(parseInt(nS) * 1000);  return d.getYear() + '/' + (d.getMonth()+1) + '/' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();}


你的 $.ajax 缺少 dataType:'json' 属性,所以返回的 data 是字符串。
作为分享这是不道德的(有意隐藏关键代码)



谢谢哈,那个是我默认不写的,之后一定严格按照格式来写 嘿嘿

function getLocalTime(nS) {  var d = new Date(parseInt(nS) * 1000);  return d.getYear() + '/' + (d.getMonth()+1) + '/' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();}


你的 $.ajax 缺少 dataType:'json' 属性,所以返回的 data 是字符串。
作为分享这是不道德的(有意隐藏关键代码)



多谢了,JSON无论是模板还是程序我都没写 以后一定注意
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn