Error: Cannot read Property 'each' of Undefined

Asked

Viewed 253 times

1

I have a Javascript code that returns values from a PHP and mounts an HTML table. You are giving the error Cannot read Property 'each' of Undefined. I looked several times without success. Could you give me a hand? Follow the code:

Javascript:

function getListaItems(idprojeto) {
  //alert(idprojeto);
	jQuery.ajax({
		type: "POST",
		url: "get-lista-items.php?idprojeto=" + idprojeto,
		//data: dataEvento,
		dataType: 'json',
		success: function(resposta) {
			var str_html = '';
			$.each(resposta, function(){
				str_html = str_html + '<tr class="gradeA" id="' + this.id + '">' +
        '<td class="center"><input type="checkbox"  id="item[]" name="item[]" onchange="changeColor(' + this.id + ')" value="' + this.id + '" /></td>' +
        '<td class="center">' + this.descricao + '</td>' +
        '<td class="center">' + this.descCategoria + '</td>' +
        '<td class="center">' + this.descCaracteristica + '</td>' +
        '<td class="center">' + this.descMedida + '</td>' +
        '<td class="center"><input type="text" id="qtd' + this.id + '" style="width:80px"/></td>' +
        '</tr>';
			});

			document.getElementById("resultJs").innerHTML = str_html;
		}
	});
}

PHP:

<?php

	session_start();
	require_once("ProjectIncludes.php");

	$service = new ProjetoxItensService();
	$consulta = $service->getAll($_GET['idprojeto']);

	$retorno = json_encode($consulta);
	echo $retorno;

?>

Thanks in advance.

  • Have you checked if your json is not coming empty in the ajax request?

  • Yeah. I already checked. It’s straight.

  • Apparently $ is undefined, somewhere on the page the $ is set as Undefined or is made a delete above the same?

  • I found the problem. I replaced it $ that comes in front of the each for jQuery.

1 answer

3

Use jQuery.each instead of $.each. Apparently the alias $ does not exist in your program.

  • Thank you. I had already found the solution in another forum. Still, thank you.

Browser other questions tagged

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