从mysql获取datetime,其中ajax返回NaN或未定义

时间:2014-11-20 20:46:34

标签: php mysql ajax

所以,这是php / mysql代码:

$postID = $_REQUEST['pIdPost'];
    $result = mysqli_query($con,
    "SELECT 

comments.IdPost, comments.IdUser, 
comments.Comment AS Comment , users.UserImage , 
users.Username ,  DATE_FORMAT(comments.CommentDate, '%d/%m/%Y %H:%i:%s') AS cDate

    FROM comments, users 

    WHERE 

    comments.IdPost = '$postID' 

    AND

    users.IdUser = comments.IdUser ")or die('Errant Query:'); 

    while($row = mysqli_fetch_assoc($result))
        {
            $output[]=$row;
        }

    header('content-type: application/json; charset=utf-8');

    print(json_encode($output, JSON_UNESCAPED_UNICODE));
    //print(json_encode($output));
    mysqli_close($con);

它为我提取了一个像这样的json数组:

[{"IdPost":"2",
"IdUser":"5",
"Comment":"Me 3",
"UserImage":"images\/defaultUser.png",
"Username":"Mia",
"cDate":"16\/11\/2014 00:01:05"}]

所有ajax功能都完美地工作,除了未识别的日期或我解析它说NAN。

这里有什么问题?我可以将它解析为一个字符串,以便它以字符串格式出现并结束吗?

若然,怎么样?

致以最诚挚的问候,

Pihh

PS:请求的ajax:

$.ajax({            
            type: "POST",
            url: "http://192.168.0.13/masonry/getSinglePost.php?pIdPost="+$(this).attr('data-item'),
            data:{},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var Comments = response;
                var $boxes;
                $.each(Comments, function (index, comment) {
                    $boxes = $(

                               '                 <span class="commentDescriptionTimeAgo">' +
/* comment.Date*/           '                     • ' + comment.cDate+
                               '                 </span>' +

                               );
                    $('#abv').append($boxes); //

1 个答案:

答案 0 :(得分:0)

$.ajax({            
        type: "POST",
        url: "test.php",
        data:{},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var Comments = response;
            var $boxes;
            $.each(Comments, function (index, comment) {
                console.log(comment);
            });
        }
    });

这在控制台中给我这个:

2
5
Me 3 
images/defaultUser.png
Mia
16/11/2014 00:01:05

所以,这段代码:

$.ajax({            
        type: "POST",
        url: "test",
        data:{},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var Comments = response;
            var $boxes;
            $.each(Comments, function (index, comment) {
                $boxes = '<span class="commentDescriptionTimeAgo">'+comment+'</span>';
                $('#abv').append($boxes); 
            });
        }
    });

输出如下:

  

25Me 3images / defaultUser.pngMia16 / 11/2014 00:01:05