Error making ajax call

Asked

Viewed 53 times

0

Hello, I’m trying to make the following call ajax:

function salvarAposta() {

    var nomeApostador = $('#nomeApostador').val();
    var jogosSelecionados = $('#tabelaFinalizacao tbody tr');
    var valorAposta = parseFloat($('#valor_aposta').val());
    var valorRetorno = parseFloat($('#valor_retorno').val());
    var listaApostas = new Array();

    if (!nomeApostador) {
        alert('Informe o nome do apostador!');
        return false;
    }

    jogosSelecionados.each(function() {
        listaApostas.push($(this).data('key'));
    });

    $.ajax({
        url: 'http:/localhost/projetos/centraljogos/webservice/aposta.php',
        type: 'GET',
        dataType: 'json',
        data: {
            nomeApostador: nomeApostador, 
            valorAposta: valorAposta, 
            valorRetorno: valorRetorno, 
            listaApostas:listaApostas
        },
        ContentType: 'application/json',
        success: function(response){
            if(response == "success"){
                alert('Aposta salva com sucesso!');                
            }
            else{
                alert('aposta -> Não foi possível salvar a aposta!');
            }
        },
        error: function(err){
            alert('aposta -> Ocorreu um erro ao se comunicar com o servidor! Por favor, entre em contato com o administrador ou tente novamente mais tarde.');
        }
    });
}

Here’s what I got on 'bet.php'':

<?php echo 'success'; ?>

The function returns me 'error' instead of 'Success', which is funny because I make other calls with the same structure and it works. Ah, the url is correct. If anyone can give me a hand I’d appreciate it.

  • 1

    has already activated the developer tools and inspected the request data made?

  • Complementing what @fernandosavio said, give console.log(err); and paste the result into the question. Does this AJAX request return which HTTP code? Have you checked if any exceptions are released in the backend?

1 answer

1


In your ajax you expect a json, but in your php function you are returning an echo (i.e., an HTML with Success in body).

I believe that if you do it will work:

header('Content-type: application/json');
echo json_encode(['success']);
exit;
  • 1

    It was exactly this detail that was missing, because I had already done it in the other functions and had let go unnoticed in this. Thank you, mate!

Browser other questions tagged

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