Showing posts with label Cancel Request. Show all posts
Showing posts with label Cancel Request. Show all posts

Saturday, January 14, 2017

Abort / Cancel jQuery Ajax Request Example




<script type="text/javascript" src="jquery-min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var $xhr = $.ajax({
            url: "search.php",
            type: "POST",
            data: {id: 1, name: "Pritom Kumar"},
            beforeSend: function(xhr){
                xhr.setRequestHeader('X-Test-Header1', 'test-value1');
                xhr.setRequestHeader('X-Test-Header2', 'test-value2');
            },
            dataType: "json"
        }).done(function(data, textStatus, jqXHR) {

        }).fail(function(jqXHR, textStatus, errorThrown) {
            alert("Failed With Reason=" + textStatus);
        });
        setTimeout(function() {
            $xhr.abort();
        }, 100);
    });
</script>