2014-11-20 14 views
0

所以,這裏是PHP/MySQL的代碼:得到的日期時間與阿賈克斯返回NaN或不確定

$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); // 
+0

你能與我們分享您的AJAX功能? – Aleksandar 2014-11-20 20:52:50

+0

我認爲這是無關緊要的,但我會發布它(該函數是巨大的 - 這是加載配置文件,所以我會粘貼重要的部分) - 其他的事情,如果我改變日期爲其他類型的值像postId它返回完美。 它在這裏! – pihh 2014-11-20 20:55:08

+0

你在你的Ajax中有'TimeAgo' /'cDate',但是我只在你返回的json數組中看到'CommentDate'的關鍵字,我錯過了一些東西。 – Sean 2014-11-20 21:12:59

回答

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 /二千零十四分之一十一0時01分05秒