I want to select some content from the database and return it to the javascript. There are several rows returned by the database. I tried this with JSON and also get a result, if I print it out. But if I want to convert the JSON string, there is always the error message below. (at the JSON.parse) So, I assume maybe an mistake while filling the array? Thanks in advance guys!
Javascript:
$.ajax({
url: "./select_firsttracks.php",
type: "post",
success: function(resultset) {
$("#erroroutput").html(resultset);
var arr = JSON.parse("{" + resultset + "}"); // --> "Uncaught SyntaxError: Unexpected token {"
},
error: function(output) {
$("#erroroutput").html("fatal error while fetching tracks from db: " + output);
}
});
PHP:
$storage = array();
while($row = $result->fetch_array())
{
$storage[] =
array
(
"id" => $row["id"],
"trackname" => $row["trackname"],
"artist" => $row["artist"],
"genre" => $row["genre"],
"url" => $row["url"],
"musicovideo" => $row["musicovideo"]
);
echo json_encode($storage);
}
Output on the console:
[{"id":"1","trackname":"yes","artist":"Lady Gaga","genre":"Pop","url":"ftp:\/development","musicovideo":"1"}][{"id":"1","trackname":"yes","artist":"Lady Gaga","genre":"Pop","url":"ftp:\/development","musicovideo":"1"},{"id":"2","trackname":"no","artist":"Prinz Pi","genre":"Rap","url":"ftp:\/development","musicovideo":"1"}]
{and}inJSON.parse("{" + resultset + "}");Code:JSON.parse(resultset);or adddataType: 'json',in ajax configurations