0
I have a json array with two property objects, an id and an email, and it has only three ojets within that array, but the number returned from the length property is 152. Can someone help me?
/*JSON*/
[{"id_cli":"1","em_cli":"[email protected]"},{"id_cli":"2","em_cli":"[email protected]"},{"id_cli":"3","em_cli":"[email protected]"}]
/*-------------------------------------------*/
/*Os arquivos PHP creio que não influenciam em nada, mas se precisar eu posto eles aqui*/
function enviarEmails() {
var titulo = document.getElementById("ti_email").value;
var email = tinyMCE.get('email').getContent();
if(titulo === "" || email === "") {
if(titulo === "") {
alert("Sem Título!");
} else {
alert("Sem Conteúdo!");
}
} else {
var formData = new FormData();
var xmlhttp = new XMLHttpRequest();
var emailsClientes;
var requisicoesDeEnvio = 0;
var emailsEnviados = 0;
var aux = 0;
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState === 4 && xmlhttp.status === 200) {
emailsClientes = xmlhttp.responseText;
console.log(emailsClientes);
formData = new FormData();
xmlhttp = new XMLHttpRequest();
formData.append("titulo", titulo);
formData.append("conteudo", email);
//Aqui retorna 152 emailsClientes.length
for(var i = 0; i < emailsClientes.length; i++) {
$.ajax({
type: "POST",
url: "enviarEmails.php",
data: {
titulo: titulo,
conteudo: email
}
}).done(function(e) {
if(e == 1) {
emailsEnviados++;
}
console.log(emailsEnviados);
});
}
}
}
xmlhttp.open("POST", "getEmailsClientes.php", true);
xmlhttp.send(formData);
}
<div id="enviarEmails">
<input type="text" name="ti_email" id="ti_email">
<textarea name="email" id="email" ></textarea>
<input type="submit" value="Enviar" onclick="enviarEmails()" />
</div>
emailsClients.length?
– Wees Smith
Yeah, I’m getting the size the wrong way?
– FiREBiRD
[{"id_cli":"1","em_cli":"[email protected]"},{"id_cli":"2","em_cli":"[email protected]"},{"id_cli":"3","em_cli":"[email protected]"}]

Isso é o que printa quando você dá um console.log(emailsClientes); ?
– Lucas Brogni
friend, a formData is different from a JSON.
– Maycon F. Castro
Lucas, yes that’s what printa
– FiREBiRD
If the json you are using is the same as the one you showed above the code the length of it really is 3.
– Maycon F. Castro
gives a console.log in xmlhttp.responseText.
– Maycon F. Castro
Um, agr I saw, I’m sending a string in json format back to this page, this must be what you’re returning, the size of the string, someone knows a Function that takes a string in this format and transfers in json back??
– FiREBiRD
Do a test ... Object.values(emailsClients). foreach(element => { console.log(element) }); );
– Lucas Brogni
Firebird.. JSON.parse(variable) ...
– Lucas Brogni
That’s exactly what it was, vlw Lucas. And malz ai kkk, beginner’s mistake.
– FiREBiRD
It happens... I’ll put the answer down there just to finish the topic.
– Lucas Brogni