Why does the array returned by ajax prompt undefined?

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:

/ *

  • 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);

Feb.10,2022

1 do you network open any returned data? If you return the data and take a screenshot of the data format
2 JSON.stringify (data) .username JSON.stringify is the object sequence into a string, so how can you use . to access, try JSON.parse (data) .username


suggest the use of markdown. Your code makes my balls hurt.
JSON.stringify (data) this method converts data to a string, and now that it is a string, you cannot call out the property in the way of an object.


https://developer.mozilla.org.

Menu