Property . length of json object returning number out of reality

Asked

Viewed 204 times

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?

  • Yeah, I’m getting the size the wrong way?

  • [{"id_cli":"1","em_cli":"[email protected]"},{"id_cli":"2","em_cli":"[email protected]"},{"id_cli":"3","em_cli":"[email protected]"}]&#xA;&#xA;Isso é o que printa quando você dá um console.log(emailsClientes); ?

  • friend, a formData is different from a JSON.

  • Lucas, yes that’s what printa

  • If the json you are using is the same as the one you showed above the code the length of it really is 3.

  • gives a console.log in xmlhttp.responseText.

  • 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??

  • Do a test ... Object.values(emailsClients). foreach(element => { console.log(element) }); );

  • Firebird.. JSON.parse(variable) ...

  • That’s exactly what it was, vlw Lucas. And malz ai kkk, beginner’s mistake.

  • It happens... I’ll put the answer down there just to finish the topic.

Show 7 more comments

1 answer

0

Passes the client email variable to JSON again with JSON.parse (variable).

JSON.parse(emailsCliente);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.