试了两天了都没解决!哪位大神可以帮忙解决下,急,在线等!ajax返回的数组为什么alert 为undefined?注:readComment.php是确定可以和数据库链接并取到正确的结果数组的,其他方法试过,用了ajax就不行了。。。
Ajax function:
$('#getComments').click(function () {
var recipe=document.title;
var session_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:
/**
Make sure the user can read the comments.
*/
require_once './resources/fragments/start.php';
use TastyRecipesControllerSessionManager;
$recipe = $_POST["recipe"];
$controller = SessionManager::getController();
$result_array = $controller->getComments($recipe);
echo json_encode($result_array);