试了两天了都没解决!哪位大神可以帮忙解决下,急,在线等!ajax返回的数组为什么alert 为undefined?Ajax function: $('#getComments').click(function () {
var recipe=document.title;
var session_username = '<%=session.getAttribute("username")%>';
$.ajax({
type: 'POST',
url: "readComment.php",
data: {"recipe": recipe},
dataType: 'json',
success: function(data){ alert (JSON.stringify(data).username);// alert undefined! }, error:function(){
alert("error");
}
});});
php file: readComment:<?php
/**
* Make sure the user can read the comments.
*/
require_once './resources/fragments/start.php';
use TastyRecipes\Controller\SessionManager;
$recipe = $_POST["recipe"];$controller = SessionManager::getController();
$result_array = $controller->getComments($recipe);echo \json_encode($result_array);
var recipe=document.title;
var session_username = '<%=session.getAttribute("username")%>';
$.ajax({
type: 'POST',
url: "readComment.php",
data: {"recipe": recipe},
dataType: 'json',
success: function(data){ alert (JSON.stringify(data).username);// alert undefined! }, error:function(){
alert("error");
}
});});
php file: readComment:<?php
/**
* Make sure the user can read the comments.
*/
require_once './resources/fragments/start.php';
use TastyRecipes\Controller\SessionManager;
$recipe = $_POST["recipe"];$controller = SessionManager::getController();
$result_array = $controller->getComments($recipe);echo \json_encode($result_array);
看看是什么
那你返回的数据如果是json,会自动转成对象,不需要你再做转换了