【问题标题】:Angular success function called before http.get function returns在 http.get 函数返回之前调用的 Angular 成功函数
【发布时间】:2018-06-16 16:33:12
【问题描述】:

我有一个角度控制器,它发出 HTTP GET 请求并在承诺返回时调用错误函数的成功。我现在的代码有时会在 GET 请求返回后执行成功函数,有时会在它之前执行。在我的承诺处理中是否有什么我搞砸了?需要注意的是,有时 http GET 甚至没有被调用(或者至少是它的断点)并且无论如何都会发生成功函数。这是我的代码:

.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);
            }
        );
    };
}

【问题讨论】:

  • 试试这个方法.then(function(success){ console.log(success.data)}, function(e) { console.log(e) }
  • @AshikBasheer 有同样的问题
  • 你在哪里调用 checkOut 函数?
  • 不要'code'和'userName'变得未定义?另外,那个网址正确吗?检查网络标签以获得实际响应
  • @lifetimeLearner007 URL 正确,变量也正确。有时请求会正确通过,但大多数时候请求永远不会通过。

标签: javascript angularjs promise angular-promise


【解决方案1】:

我发现这是因为 Angular 将 GET 请求缓存了一段时间,而我的请求的缓存版本正在被使用。

【讨论】:

    猜你喜欢
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多