0

I need to make a http request depeendent upon the response of a json object. I have the specific string in the $scope.id object. My code is this

app.controller('test', function($scope, $http) {
  $http.post("http://127.0.0.1/flat.json", {"key": "value"})
  .then(function (response) {$scope.id = response.data.id;});
});

I want to add another http request that would be:

http://127.0.0.1/{{id}}

I have tried everything that I can think of to generate this request, but i either get "undefined" variable or the script fails to execute.

0

1 Answer 1

1

Double brackets are reserved for the view. When working with javascript, you can simply use string concatenation to create your url.

app.controller('test', function($scope, $http) {
  var path = 'theendoftheurl'

  $http.post("http://127.0.0.1/" + path , {"key": "value"})
  .then(function (response) {$scope.id = response.data.id;});
});

Edit: If I'm understanding your question correctly, you want to do something along these lines:

app.controller('test', function($scope, $http) {
      $http.post("http://127.0.0.1/flat.json" , {"key": "value"})
      .then(function (response) {
         $http.post("http://127.0.0.1/" + response.data.id, {})
         .then(function (secondResponse) {
           //secondResponse is the data you're looking for
         });
      });
    });
Sign up to request clarification or add additional context in comments.

Comments

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.