0

I have an angular controller which makes an HTTP GET request and on the promise return, calls either a success of error function. My code right now will sometimes execute the success function after the GET request returns and sometime execute before it. Is there something I'm messing up in my promise handling? Something to note, sometimes the http GET doesn't even get called (or at least its breakpoint) and the success function happens regardless. Here is my code:

.controller('CheckOutCtrl', ['$scope', '$http', function($scope, $http) {
    //Controller for checkout feature
    $scope.owner = "Unclaimed"; //Default owner
    $scope.endDate = "";
    $scope.unclaimed = true;
    $scope.showpopup = false; //Should we show the popup that checkout is successful or returned
    $scope.currentOwner = false; //Show return date
    $scope.popuptext = "";

    $scope.checkOut = function(){ //Extends or creates a check out
        $http.get("/checkout/extend/code/" + code + "/username/" + userName).then(
            function success(response, status, headers, config) {
                console.log("Checked out or extended");
                $scope.showpopup = true;
                $scope.currentOwner = true;
                $scope.popuptext = response.data; //Show airport checked out until or error
                console.log($scope.popuptext);
                init(); //Update current owner
            },
            function error(response, status, headers, config) {
                $scope.error = response;
                console.error("Check out error:", response);
            }
        );
    };
}
8
  • try this way instead .then(function(success){ console.log(success.data)}, function(e) { console.log(e) } Commented Jun 16, 2018 at 16:39
  • @AshikBasheer That has the same issue Commented Jun 16, 2018 at 16:45
  • where do you call checkOut function? Commented Jun 16, 2018 at 17:05
  • Don't 'code' and 'userName' become undefined? Also, is that URL correct? Check network tab for actual response Commented Jun 16, 2018 at 17:11
  • @lifetimeLearner007 The URL is correct, as are the variables. Sometimes the request goes through correctly, but most times the request is just never put through. Commented Jun 16, 2018 at 17:22

1 Answer 1

0

I discovered this was happening because Angular caches GET requests for some time and the cached version of my request was being used.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.