I have a really annoying problem and I do not know why.
Through an ajax request, I get the server notifications that the user has not yet viewed (similar to facebook) For this, I use the following code:
$.ajax({
url: BASE_URL+"ajax/getNotifications",
type: "POST",
dataType: "json",
success: function(response){
var resultHTML = '';
response.forEach(function(result)
{
resultHTML += '<a href="'+result.href+'" class="no-ajaxy">\n' +
' <div class="user-box">\n' +
' <div class="user-foto"></div>\n' +
' <div class="name">'+result.titulo+'</div>\n' +
' <div class="mail">'+result.mensagem+'</div>\n' +
' </div>\n' +
' </a>';
});
$("#justA").hide().html(resultHTML).fadeIn("slow");
},
error: function(error) {
console.log(error + " - teste");
}
});
It works correctly when the result in json causes more than one notification (which is added dynamically as you can see)
But when I only get a single notification, I get the following error:
Uncaught TypeError: response.forEach is not a function
I do not know if javascript is different, but in PHP for example, foreach works if it has one or more elements, but in js it seems to work only when it actually has more than one element What can be done? Thank you
return where forEach does not work:
{"href":"javascript:void(0)","titulo":"Sem notifica\u00e7\u00f5es!","mensagem":"Atualmente voc\u00ea n\u00e3o possu\u00ed notifica\u00e7\u00f5es"}
Return on how it works:
[{"href":"\/navegar","titulo":"teste horario 04:09","mensagem":"TESTE HORARIO 04:09"},{"href":"pregao\/ativos\/detalhes\/6","titulo":"Preg\u00e3o Finalizado!","mensagem":"Seu preg\u00e3o foi finalizado, confira!"}]