Answer the question
In order to leave comments, you need to log in
Why not parse the JSON response?
Hey! I can not parse the json response for the umpteenth time.
Answer:
{"id":"1","fio":"hhhhhh","pn":"1","vt":"1","sr":"1","cht":"1 ","pt":"2","sb":"1","vs":"1","user_id":"154","group_id":"0","data_reg":"2015-11 -18"}{"id":"2","fio":"sdfsdfsdf","pn":"2","vt":"3","sr":"1","cht":" 2","pt":"3","sb":"1","vs":"2","user_id":"154","group_id":"0","data_reg":"
if ( $_GET["func"] == "GetPersonal" )
{
$result = $mysqli->query( "SELECT * FROM personal WHERE user_id='".$_SESSION['user_id']."'" );
if (!$result) error($mysqli->error);
if ( $result->num_rows > 0 )
{
while($row = $result->fetch_assoc())
{
$myArray[] = $row;
}
echo json_encode($row);
}
else
{
echo json_decode( array( "answer" => "empty" ) );
}
$result->free();
$result->close();
$mysqli->close();
exit();
}
Answer the question
In order to leave comments, you need to log in
you do not need to output each row, but collect them in an array and call json_encode once at the end of the script.
if ( $result->num_rows > 0 )
{
while($row = $result->fetch_assoc())
{
$myArray[] = $row;
}
echo json_encode($myArray);
}
jsfiddle.net/zdbhvdr5
You {...}{...}
should have a [{...},{...}]
Replace
while($row = $result->fetch_assoc()) {
$myArray[] = $row;
echo json_encode($row);
}
while($row = $result->fetch_assoc()) {
$myArray[] = $row;
}
echo json_encode($myArray);
The thing is that in your JSON (or rather, this is not a correct json) several objects are displayed in a row, you can’t do this. They are not part of a large whole and, accordingly, cannot be parsed together..enes..senes? converted to a JavaScript entity. You need to change the answer to:
[
{"id":"1","fio":"hhhhhh","pn":"1","vt":"1","sr":"1","cht":"1","pt":"2","sb":"1","vs":"1","user_id":"154","group_id":"0","data_reg":"2015-11-18"},
{"id":"2","fio":"sdfsdfsdf","pn":"2","vt":"3","sr":"1","cht":"2","pt":"3","sb":"1","vs":"2","user_id":"154","group_id":"0","data_reg":"2015-11-18"}
]
if ( $_GET["func"] == "GetPersonal" ){
$result = $mysqli->query( "SELECT * FROM personal WHERE user_id='".$_SESSION['user_id']."'" );
if (!$result) error($mysqli->error);
if ( $result->num_rows > 0 ) {
while($row = $result->fetch_assoc()) {
$myArray[] = $row;
}
echo json_encode($myArray);
} else {
echo json_decode('{"answer" : "empty"}');
}
$result->free();
$result->close();
$mysqli->close();
exit();
}
var obj = JSON.parse(data);
obj.forEach(function(current){
console.log(currenr.fio);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question