【问题标题】:Chaining the $http.get AngularJS链接 $http.get AngularJS
【发布时间】:2019-12-02 17:55:26
【问题描述】:

由于我很少调用函数中的端点,并且导致对端点的并行调用出现问题,因此在另一个问题中建议使用承诺链。我更新了我的代码,以便我们可以一个接一个地调用端点,所以代码如下所示

 $scope.getRequest = function () {
        var url = $rootScope.BaseURL;
        var config = {
            headers: {
                'Authorization': `Basic ${$scope.key}`,
                'Prefer': 'odata.maxpagesize=2000'
            }
        };
        $http.get(url, config)
            .then(newViewRequest)
            .then(function(response){ 
                $scope.viewRequest.data = response.data;
            },
            function (response) { // failure async
                console.log("There was an error getting the request from CORE");});
   };

    var newViewRequest = function (response) {
        var url1 = $rootScope.BaseURL + `CMQ_REQUEST('${$scope.viewRequest.barcode}')`;
        if (response.data.REV_SAMPLE_CMQREQUEST.length = 0) {
            return $http.get(url1, config)
         }
        return $q.reject({ message: 'Validations didnt work' });
    };

如果response.data.REV_SAMPLE_CMQREQUEST.length = 0,它总是从 newViewRequest 发回拒绝消息,如果我将其注释掉,我会得到 response.data 未定义。

【问题讨论】:

    标签: javascript angularjs http-get


    【解决方案1】:

    更新您的条件以验证而不是分配

    问题: 更新 if 条件如下以检查 response.data.REV_SAMPLE_CMQREQUEST.length 是否为 0,使用 === 而不是 =

    if (response.data.REV_SAMPLE_CMQREQUEST.length === 0) 
    

    【讨论】:

    • 谢谢,但问题是我没有在 If 条件下通过 $http.get 调用得到响应。我添加了截图
    • @trx,尝试 console.log response1
    • 日志看起来不错。由于 response.data 未定义,如何从 newViewRequest 获取响应到 $scope.viewRequest.data
    • 请分享 console.log 以获取 response1
    • 更新截图
    猜你喜欢
    • 2017-05-10
    • 2018-01-27
    • 1970-01-01
    • 2015-05-30
    • 2016-03-29
    • 1970-01-01
    • 2013-04-11
    • 2014-05-03
    • 1970-01-01
    相关资源
    最近更新 更多