Showing posts with label ajax. Show all posts
Showing posts with label ajax. Show all posts

Friday, March 30, 2018

What's the best way to retry an AJAX request on failure using jQuery | The best way to retry an AJAX request on failure using jQuery | jQuery AJAX retry

$.ajax({
    url: 'http://',
    type: 'POST',
    data: {},
    tryCount: 0,
    retryLimit: 3,
    success: function (response) {
        //do something
    },
    error: function (xhr, textStatus, errorThrown) {
        this.tryCount++;
        if (this.tryCount <= this.retryLimit) {
            //try again
            $.ajax(this);
            return;
        }
        if (xhr.status == 500) {
            //handle error
        }
        else {
            //handle error
        }
    }
});

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>


Friday, November 4, 2016

Angularjs ajax get or post method example from controller


app.controller('TestController', function($scope, $http) {
    $scope.name = "Pritom Kumar";
    $scope.posts = ["a", "b"];
    
    $http.post('lists_json', {id: 1, name: $scope.name}).
    success(function(response, status, headers, config) {
        console.log("Success=" + response);
    }).
    error(function(response, status, headers, config) {
        console.log("Error=" + response);
    });
    
    $http.get('lists_json?id=1&name='+$scope.name+"&list="+$scope.posts).
    success(function(response, status, headers, config) {
        console.log("Success=" + response);
    }).
    error(function(response, status, headers, config) {
        console.log("Error=" + response);
    });
});

Friday, January 18, 2013

jQuery post call using ajax

jQuery.ajax({
    type: "POST",
    url: "http://domain.com/save_form_data",
    data: jQuery("form").serializeArray(),
    success: function(data) {
        console.log(data);
        alert("Success");
    },
    error: function() {
       alert("Error occurred."); 
    }
});