AngularJS:多个$ http请求和.then promises返回undefined

时间:2015-12-15 09:51:22

标签: javascript angularjs json http promise

在尝试使用AngularJS连续发出两个http请求后,我没有得到第二个http请求的响应。

我有这段代码:

        createBucket: function (bucketKey, policyKey) {

            // get token from json file
            return $http({
                url: tokenUrl
            })
            .then(function (response) {
                token = response.data;
                console.log('refreshing access token');
                console.log(token);
            })
            .then(function (response) {

                // use token to create new bucket
                $http({
                    method: 'POST',
                    headers: {
                        "Authorization": token.token_type + " " + token.access_token
                    },
                    url: "https://developer.api.autodesk.com/oss/v2/buckets",
                    data: {
                        "bucketKey": bucketKey,
                        "policyKey": policyKey
                    }

                });

            }).then(processResponse);
        },

首先我做一个获取json文件的http请求。然后使用这个json文件中的信息,我再做一个http请求,这个请求的结果我要返回。在最后的代码中:

    // Send the data part of the response
    function processResponse(response) {
        console.log('response:');
        console.log(response);
        return response.data;
    }

这里的回复是 undefined ..我不知道为什么......

4 个答案:

答案 0 :(得分:2)

您需要返回值才能进行链接

createBucket: function (bucketKey, policyKey) {

    // get token from json file
    return $http({
        url: tokenUrl
    })
    .then(function (response) {
        var token = response.data;
        console.log('refreshing access token');
        console.log(token);
        //return token for chaining
        return token;
    })
    .then(function (token) {
        //save 2nd httpPromise for chaining
        var p1 = $http({
            method: 'POST',
            headers: {
                      "Authorization": token.token_type + " " + 
                      token.access_token
                      },
            url: "https://developer.api.autodesk.com/oss/v2/buckets",
            data: {
                "bucketKey": bucketKey,
                "policyKey": policyKey
            }
        //return httpPromise for chaining
        return p1;
        });

    }).then(processResponse);
},

答案 1 :(得分:1)

你有太多then ...当你链接then语句时,请确保每个语句都返回下一个语句的保证。尝试:

createBucket: function (bucketKey, policyKey) {

    // get token from json file
    $http({
        url: tokenUrl
    })
    .then(function (response) {
        token = response.data;
        console.log('refreshing access token');
        console.log(token);
        return $http({
            method: 'POST',
            headers: {
                "Authorization": token.token_type + " " + token.access_token
            },
            url: "https://developer.api.autodesk.com/oss/v2/buckets",
            data: {
                "bucketKey": bucketKey,
                "policyKey": policyKey
            }
        });
    })
    .then(processResponse);
}

答案 2 :(得分:0)

您的每个.then都需要返回一些内容:

    createBucket: function (bucketKey, policyKey) {

        // get token from json file
        return $http({
            url: tokenUrl
        })
        .then(function (response) {
            token = response.data;
            console.log('refreshing access token');
            console.log(token);
            return response;
        })
        .then(function (response) { // Response was already undefined here. The reason why token had something is because you surely have defined it in a parent javascript scope.

            // use token to create new bucket
            $http({
                method: 'POST',
                headers: {
                    "Authorization": token.token_type + " " + token.access_token
                },
                url: "https://developer.api.autodesk.com/oss/v2/buckets",
                data: {
                    "bucketKey": bucketKey,
                    "policyKey": policyKey
                }
            });
            return response;

        }).then(processResponse);
    },

答案 3 :(得分:0)

在链接承诺时,您需要从每个 .then 步骤返回一个新承诺,以便为后续的每个步骤执行。