0
I’ve got a very boring problem and I don’t know why
Through an ajax request, I get from the server the 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 json result brings back more than ONE notification (which is added dynamically as you can see)
But when it only brings a single notification, I have the following error:
Uncaught Typeerror: Response.foreach is not a Function
I don’t know if javascript is different, but in PHP for example, foreach works if you have one or more elements, but in js it seems to work only when you actually have more than one element What can be done? Thank you
return where the 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 in which 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!"}]
Edith the question and put the return, so someone help you.
– NoobSaibot
@wmsouza I edited
– Everton Neri