Ajax request does not work

Asked

Viewed 498 times

0

I have 3 files:

novaDose.php:

<label for="busca">Buscar cidadão:</label>
      <input type="text" class="form-control" id="busca" placeholder="Digite parte do nome ou o CNS" onkeypress="busca(this.value);">
      <br/>
      <table class="table table-bordered">
        <thead>
          <tr>
            <th scope="col">CNS</th>
            <th scope="col">Nome Completo</th>
            <th scope="col">Data de Nasc</th>
          </tr>
        </thead>
        <tbody id="tabelaPaciente">
          <?php
            include 'conexao.php';
            $result = mysqli_query($con, "select * from paciente limit 20");

            while ($item = mysqli_fetch_assoc($result)){
              $dataNasc = date_create($item["dataNasc"]);
              echo '<tr>
                      <td>'.$item["cns"].'</td>
                      <td>'.$item["nomePaciente"].'</td>
                      <td>'.date_format($dataNasc, "d/m/Y").'</td>
                    </tr>';
            }
          ?>
        </tbody>
      </table>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="novaDose.js"></script>

searchPHP:

include 'conexao.php';

$result = mysqli_query($con, "select * from paciente");

echo json_encode($result);

mysqli_close($con);

novaDose.js:

function busca(){
$('#tabelaPaciente').empty(); //ATÉ AQUI FUNCIONA
$.ajax({
    type:'post',
    dataType:'json',
    url:'buscar.php',
    success: function(dados){
    alert(dados); //ESSE ALERTA NÃO FUNCIONA
        for(var i=0;dados.length>i;i++){
            //Adicionando registros retornados na tabela
            $('#tabela').append('<tr><td>'+dados[i].cns+'</td><td>'+dados[i].nomePaAciente+'</td><td>'+dados[i].dataNasc+'</td></tr>');
        }
    }
});
};

Apparently everything works ok, but I can not succeed in the requisition. Use firefox and in the console and network debug modules are not returning any error.

  • 1

    if it doesn’t work it’s because it doesn’t work from Success, invert the echo line with mysql close.

  • @Juliohenrique same thing

  • 1

    puts this on top of echo . header('Content-type: application/json');

  • @Juliohenrique still, with no return. see that I have an Alert(data) in the Success field but it does not run. how can I show any error?

1 answer

2


I believe you are missing a header in your search.php and the result needs fetch

<?php 
header('Content-Type: application/json');

include 'conexao.php';

$result = mysqli_query($con, "select * from paciente");

echo json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC));

mysqli_close($con);

?>

In your AJAX code

 dataType:'json',

This means the request is expecting a JSON as response, but without that header in your php code, the server will respond an HTML.

Jquery slim does not contain AJAX. Use the complete:

https://code.jquery.com/jquery-3.3.1.min.js

  • Still, no return. see that I have an Alert(data) in the Success field but it does not run. how can I show any error?

  • 1

    You tried to apply echo json_encode(mysqli_fetch_all($result, MYSQLI_ASSOC)); ?

  • if I directly access search.php the data appears correctly. but it is not yet listed in the table

  • 1

    For your go, try this for(var i=0; i < Object.Keys(data). length ;i++)

  • before the is, has an Alert. and it is not even running...

  • 2

    If I’m not mistaken jquery slim has no ajax. Try using the full.

  • That’s right Luan. can change your question to earn the points. Saved my day kkkkk

  • 1

    Damn, strange is the console does not show errors when using ajax :/

  • That’s right, I don’t know if I’ve disturbed something, but it gets really bad to correct without knowing what the mistake is :/

Show 4 more comments

Browser other questions tagged

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