简体   繁体   English

在AngularJS中发送多个具有更改参数的GET请求

[英]Sending multiple GET requests with changing parameter in AngularJS

Edit : Apologies to the answerers, it turns out that this was actually valid code, but requests were being intercepted and stripped of all of the parameters. 编辑 :对回答者表示歉意,事实证明这实际上是有效的代码,但是请求被拦截并剥夺了所有参数。


I'm trying to make repeated HTTP GET requests to a REST API, depending on the output and have used the solution from this question . 我正在尝试对REST API进行重复的HTTP GET请求,具体取决于输出,并已使用此问题的解决方案。

However, I wish to increment one of the parameters that I pass in the request. 但是,我希望增加我在请求中传递的参数之一。 Essentially, the API pages the output and I need to increase the value of startAt accordingly. 本质上,API分页输出,我需要相应地增加startAt的值。

Manual requests work fine with: 手动请求可以正常使用:

<URL>/board?startAt=50

And give back: 并退还:

{"maxResults":50,"startAt":50,"isLast":true,"values":[list_of_values]}

Here's my code so far: 到目前为止,这是我的代码:

function getHttpPromise(start_at) {
    // This function recurses until the server returns isLast = true.
    //
    // Each iteration appends the values in response.values to
    // $scope.boards.
    test = $http({
                    url: 'boards',
                    method: 'GET',
                    params: {'startAt': start_at.toString()}
                 }).
        success(function (response) {
            console.log(response); // the response contains startAt, which always has 
                                   // the initial value (0), rather than start_at's value

            var values = response.values;
            for (var i in values) {
                var board = values[i];
                $scope.boards[board.id] = board;
            }

            if (response.isLast) {
                // We have received all the boards.
                return true;
            } else {
                // Increment start_at and return another http request promise.
                start_at += response.maxResults;
                return getHttpPromise(start_at);
            }
        }
    );

    console.log(test); // params is correct here

    return test;
}

This function is called by: 该函数的调用者:

jiraWorkLog.controller('SprintSelectCtlr',
function($scope, $http, $routeParams) {
    $scope.init = function() {
        $scope.boards = new Object();

        getHttpPromise(0).then(
            function (dummy_var) {
            for (var board in $scope.boards) {
                ...
            }
        }
    );
}
...
);

The $http().success() is deprecated. $http().success()已弃用。 You should use $http().then() . 您应该使用$http().then()

The then receives a function than will be passed a response object that has a property called data . then该函数将接收一个函数,该函数将被传递一个具有名为data的属性的response对象。 There you'll find your values . 在这里您将找到自己的values

You can read further here 您可以在这里进一步阅读

After reading your code more thoroughly, I must advise you not to solve this problem recursively. 彻底阅读代码后,我必须建议您不要递归解决此问题。 If you don't want the data paged, send a request for all the records upfront. 如果您不希望分页数据,请提前发送所有记录的请求。

Now to answer why you only get the first result: it's because that's the only thing that is returned and the calling controller. 现在回答为什么只得到第一个结果:这是因为这是唯一返回的结果,并且是调用控制器的结果。 You are returning a promise that the controller is waiting to be resolved. 您返回的承诺是控制器正在等待解决。 All other recursive calls happen only after your controller is already done. 所有其他递归调用仅在您的控制器已经完成之后才发生。

the response holds all the http stuff and you want to get the data out of it... 响应包含所有http内容,您想从中获取数据...

I think that the following link might help you Angular Http 我认为以下链接可能对您有所帮助Angular Http

another issue that you have with your recursion is that you're updating a local variable and use it wrongly... 递归的另一个问题是您正在更新局部变量并错误地使用了它。

function getHttpPromise(start_at) {
// This function recurses until the server returns isLast = true.
//
// Each iteration appends the values in response.values to
// $scope.boards.
test = $http({
                url: 'boards',
                method: 'GET',
                params: {'startAt': start_at.toString()}
             }).
    success(function (response) {
        console.log(response.data); // the response contains startAt, which always has 
                               // the initial value (0), rather than start_at's value

        var values = response.data;
        for (var i in values) {
            var board = values[i];
            $scope.boards[board.id] = board;
        }

        if (response.data.isLast) {
            // We have received all the boards.
            return true;
        } else {
            // Increment start_at and return another http request promise.
            return getHttpPromise(response.data.maxResults+1);
        }
    }
);

console.log(test); // params is correct here

return test;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM