Success: Function AJAX does not work

Asked

Viewed 537 times

0

I have this function in JQUERY and AJAX, it executes what I want, however, do not enter in sucess to display the successful msg and reset the form, it is sending the image to the server and the form data, the only problem is this, not the return of success.

$("#form-fichaanimal").submit(function(){

    var formulario = document.getElementById('form-fichaanimal');
    var formData = new FormData(formulario);

    $.ajax({
        url  : 'code-source/insert/ins-fichaanimal.php',
        type : 'POST',
        data : formData,
        dataType: 'json',
        processData: false,  
        contentType: false,
        success: function(response){                      
            if(response.codigo == "1"){ 
                $("#btn-cadastrar-fichaanimal").html('Cadastrar');
                $("#mensagem").html(response.mensagem);
                $("#mensagem").addClass("alert alert-success");
                $("#altura").val("");
                $("#peso").val("");
                $("#sanidade").val("");
                $("#nutricao").val("");
                $("#data_ficha").val("");
                $("#modalidade").val("");
                $("#arquivo").val("");
                $("#cpf-proprietario-animal").val("");
                $("#nome").val("");
                $("#animal").val("");
            }

            else{           
                $("#btn-cadastrar-fichaanimal").html('Cadastrar');
                $("#mensagem").html("<strong>Erro: </strong>" + response.mensagem);
                $("#mensagem").addClass("alert alert-danger");
            }
        }
    });

    event.preventDefault();
    $('html, body').animate({scrollTop: 0}, 900);
    event.stopPropagation();

    setTimeout(function(){ 
    $('#mensagem').html("");
    $('#mensagem').removeClass();
    }, 5000);
});

My return code in the PHP class

$retorno = array('codigo' => 1, 'mensagem' => 'Cadastro realizado!');
    echo json_encode($retorno);
  • before returning, you set the response header to indicate that it is a json? so for example: header('Content-Type: application/json'); confirm this

1 answer

1

Apparently your problem is on the line that defines the expected return type:

{dataType:"json"}

However what Voce mentioned that your backend delivery is not a "json".

Delete the dataType line. Or switch to "string" as the expected returns are: (xml, json, script, or html)

source of information: jquery dataType

  • Answer yes, the dataType he defined in Ajax is for a JSON return, but the composition of the return he structured in the backend is not a JSON... Thank you, You’re welcome

  • 1

    Okay? I’m new here, I’m just trying to help...

  • It improved a little bit. I am not the one caught in the foot, it is a system that evaluates each answer and forwards to an analysis queue. every time a message has the link Of Revision you can be sure that it comes from analysis queue.

  • 1

    What could I do to improve?

  • That’s exactly what you’re doing. Refining content and ballasting information on trusted documentation links.

  • 1

    Thanks for helping, but I managed to add the beforeSend event: Function(){} before the sucess

Show 1 more comment

Browser other questions tagged

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