How to check ajax request is completed in jQuery

You can check ajax request is completed in jQuery using complete() and always() method. The .always() method replaces the deprecated .complete() method in latest versions of jQuery.

Answers 1
  • Ajax post request with done, fail and always method

    <script> 
    $.ajax({
        url: "/ajax.php",
        method: "POST",
        data:{
            id: 1
        }
    })
    .done(function(resp) {
        console.log(resp);
    })
    .fail(function(xhr, status, error) {
        alert(xhr.statusText);
    })
    .always(function(jqXHR, textStatus) {
        if (textStatus != "success") {
            alert("Error: " + jqXHR.statusText); //error is always called .statusText
        } else {
            alert("Success: " + jqXHR.response); //might not always be named .response
        }
        alert('AJAX call complete');
    }); 
    </script>
    
    0
  • Back to code snippet queries related jquery