How to execute a function after ajax success?

0

In my project, in the ajax success, I'm doing an autocomplete, so I need to execute a function after selecting an option, that is, after success. Would it be possible / How would I do it?

    
asked by anonymous 26.03.2018 / 20:14

1 answer

3

After success: you have complete: but it is fired both on success and error, so you need to value xhr.status to verify that the request has completed successfully. Here in snnipet it always returns error, but it is possible to demonstrate the order of events.

$(document).ready(function() {
  $.ajax({
    url: "#",
    error: function() {
      console.log("Falhou!");
    },
    success: function(data, xhr, textStatus) {
      console.log("Sucesso!");
    },
    complete: function(xhr, textStatus) {
      console.log("Completo -> " + xhr.status + " - " + textStatus);
    }    
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    
26.03.2018 / 21:30