AJAX request not working

Asked

Viewed 27 times

0

I have a function that calls an ajax to save in the bank. All my other POST requests I did this way and it worked. Now I don’t know what happened. If anyone can help me.

                $.ajax({

                    type: "GET",
                    dataType: "JSON",
                    async: false,
                    url: "passageiro_ajax.php?case=salvarPassageiro",
                    data: "prefixo=" + prefixo_id + "&linha=" + linha + "&dia=" + dia + "&hora=" + hora + "&nomeFinal=" + nomeFinal + "&catraca=" + catraca + "&catracaFinal=" + catracaFinal + "&dinheiro=" + dinheiro + "&isento=" + isento + "&transporte=" +transporte + "&estudante=" + estudante + "&livre=" + livre,
                    success: function(d) {
                        alert("Amostragem de passageiro feita com sucesso ");
                        window.location.reload();
                    },
                    error: function(d) {

                    }
                });

There on the ajax page. passageiro_ajax.php

case 'savePassageiro':

    $linha = $_POST['linha'];
    $prefixo = $_POST['prefixo'];
    $dia = $_POST['dia'];
    $hora = $_POST['hora'];
    $nomeFinal = $_POST['nomeFinal'];
    $catraca = $_POST['catraca'];
    $catracaFinal = $_POST['catracaFinal'];
    $dinheiro = $_POST['dinheiro'];
    $isento = $_POST['isento'];
    $transporte = $_POST['transporte'];
    $estudante = $_POST['estudante'];
    $livre = $_POST['livre'];


    $querySalvar = "insert into amostragem_passageiro
     (linha_id, prefixo_id, dia, hora, nome_final, catraca, catraca_final, dinheiro, isento, transporte,
      estudante, livre , usuario_id) 
     values ('{$linha}','{$prefixo}', '{$dia}','{$hora}','{$nomeFinal}','{$catraca}','{$catracaFinal}',
     '{$dinheiro}','{$isento}','{$transporte}','{$estudante}','{$livre}''{$usuario}')";


    mysqli_query($conexao, $querySalvar);

    echo json_encode($prefixo);

    break;

I do this echo to return and always returns null, I’ve tried everything Somebody help me?

1 answer

1

There are some things you can change there:

You are making the AJAX call as "GET" but it is receiving in php as POST ($_POST['line']...), you have to define if you will work with GET or POST;

Another thing, you are passing the date as query param, in which case you can put it straight as a list of objects:

  data: { 
    prefixo: prefixo_id, 
    linha: linha, 
    dia: dia,
    //...

  },

I hope I’ve helped

Browser other questions tagged

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