1

I'm new over here in stackoverflow.

Well I have a problem with my code and i couldn't find a solution, so I decide to ask my question here.

HTML

<body ng-app="app">
    <div ng-controller="ctrl">
    </div>

<script src="~/Scripts/angular.min.js"></script>

I included ng-app="app" in my body and I installed angularjs as well.

I added a javascript file in my application called ang.js

ang.js

var app = angular.module('app', [])
app.controller("ctrl", function ($http) {
    $http.get("url1")
    .success(function (response){
        console.log(response);
    })
    $http.get("url2", {
        params: {
            Id: response[0].key
        }
    .success(function (response2) {
        console.log(response2);
       })
    })
    })

My question is in

.success(function (response){
        console.log(response);
    })

How can i call this parameter response in the second one.

$http.get("url2", {
        params: {
            Id: response
        }

1 Answer 1

1

You need to make the second request in the success handler of the first, ie

var promise = $http.get('url1').then(function(res1) {
    return $http.get('url2', {
        params: {
            Id: res1.data[0].key
        }
    }).then(function(res2) {
        console.log(res2.data);
        return res2.data;
    })
});

Note that I've used then instead of the deprecated success callback.

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

1 Comment

Thanks for answering my question.

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.