Javascript 返回未定义对象的Ajax数据

Javascript 返回未定义对象的Ajax数据,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,jquery将转到a.php来检索数据,并且它成功地检索了信息,但是当信息放在页面上时,返回的是未定义的信息 var userId = $("#userid").val(); $(function() { $.ajax({ url: "../apipages/getprofilecomment.php?userId=${userId}", method: "GET", type: "json", cache: false, success: function(comment

jquery将转到a.php来检索数据,并且它成功地检索了信息,但是当信息放在页面上时,返回的是未定义的信息

var userId = $("#userid").val();

$(function()
{
$.ajax({
  url: "../apipages/getprofilecomment.php?userId=${userId}",
  method: "GET",
  type: "json",
  cache: false,
  success: function(comment){
       $('#usercommentdiv').append("<li>" + comment.user_name + ':' + comment.profile_comment + ',' + comment.time_added + "</li>");
  }
});
});

注意:用户名为空并不特别重要。

在ajax设置中将
类型:“json”
更改为
数据类型:“json”
,解析将自动进行。

控制台中是否有错误?我认为您可能输入了错误的URL。check使用警报返回了注释。警报(JSON.stringify(comment));如果所有数据都存在,请尝试添加comment=JSON.parse(comment);在append之前。它返回数据,但当放入comment.table_name时,它不起作用。它返回为未定义。警报反馈了数据。
<?php
include ("../db/database.php");
include ("../classes/profilecommentclass.php");
include ("../classes/userclass.php");

session_start();

$userId = $_SESSION['user_id'];
$userClassHandler = new User($db, $userId);
$profileCommentHandler = new ProfileComment($db);
$userNameData = $userClassHandler->getUserName();
$profileComment = $profileCommentHandler->getAllFromComment(34);
$profileCommentArray = array(
"userName" => $_SESSION["user_name"],
"user_profile_comment" => $profileComment["profile_comment"],
"time_added" =>  $profileComment["time_added"],
"time_updated" =>  $profileComment["time_updated"]
);
echo json_encode($profileCommentArray);
?>
{"userName":"Acidify","user_profile_comment":"Working to get this app running!!!","time_added":"2019-12-14 00:51:04","time_updated":"15:00:00"}