0
Good afternoon
I’m testing automate the search for record to the database with the script below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="public/css/teste.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<!-- <script src="public/js/exibi.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(function() {
var refreshAutomatico = setInterval(function(){
$('tabela').load(Teste());
}, 5000);
});
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-6">
<div id="resposta">
<table border="1" width="500">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Acesso</th>
</tr>
</thead>
<tbody id="tabela">
</tbody>
</table>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(
function Teste(){
$.ajax({
type:'post', //Definimos o método HTTP usado
dataType: 'json', //Definimos o tipo de retorno
url: 'consulta.php',//Definindo o arquivo onde serão buscados os dados
success: function montaTabela(dados){
console.log(dados);
for(var i=0;dados.length>i;i++){
//Adicionando registros retornados na tabela
$('#tabela').append('<tr><td>'+dados[i].id+'</td><td>'+dados[i].nome+'</td><td>'+dados[i].acesso+'</td></tr>');
}
}
});
});
</script>
</body>
</html>
However, the error below is occurring:
Uncaught Referenceerror: Test is not defined at test.php:15
The first tag
<script>
is executed and the methodTeste()
has not yet been instantiated, so gives this error. Passes the method inside the second tag<script>
.– iamdlm
Place your scripts after the tag
</body>
, and take advantage to adjust the loading order of your scripts.– Edilson
@Edilson tried the two solutions but still keeps giving the same error: both if I put the Function of the setInterval block after or before the ajax block
– Bruno Ferreira