I make an AJAX request to a PHP file as follows:
function verifica() {
var meuid = $('.meuid').attr('id');
var datas = "user=" + meuid;
$.ajax({
type: "GET",
url: 'sys/stream2.php',
data: datas
}).done(function(data) {
//alert(data);
$('#nome').html(data);
});
}
In my PHP file, I output JSON data from a loop:
foreach ($gUsuarios as $usuarios) {
$agora = $usuarios['AGORA'];
if ($agora >= $usuarios['cUsu_Limite']) {
echo json_encode(array('usuarioon' => $usuarios['cUsu_ID'], 'status' => 'fa fa-circle-o text-red'));
} else {
echo json_encode(array('usuarioon' => $usuarios['cUsu_ID'], 'status' => 'fa fa-circle-o text-green'));
}
}
The output is two JSON objects:
{"usuarioon":"1","status":"fa fa-circle-o text-red"}
{"usuarioon":"3","status":"fa fa-circle-o text-red"}
How can I parse the two JSON objects in my AJAX success handler? I've tried with parseJSON
, but it did not work. I think it's because I'm returning an array within a foreach
.