I"ve been trying for two days, but I haven"t solved it! Which god can help solve the problem, urgent, online and so on! Why is alert undefined? in the array returned by ajax Note: readComment.php is sure to link to the database and get the correct array of results. Other methods have been tried, but not with ajax.
Ajax function:
$("- sharpgetComments") .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 TastyRecipesControllerSessionManager;
$recipe = $_ POST ["recipe"];
$controller = SessionManager::getController ();
$result_array = $controller- > getComments ($recipe);
echo json_encode ($result_array);