Vinh.TV
Vinh.TV

Reputation: 309

angularjs how to catch http response data that sent by browser

As title, I want to catch Http response that sent by the browser.
Let say a redirect to "http://domain/api/something", actually, a GET request to "http://domain/api/something" which return a JSON data.
How can I get that data on first load using AngularJs?

Upvotes: 0

Views: 965

Answers (2)

Jaldhi Oza
Jaldhi Oza

Reputation: 92

You should modify your code as below

app.service('feedbackService', function ($http) {
   this.getFeedbackPaged = function () {
      return $http.get('http://domain/api/something');
   };
});

app.controller('feedbackController', function ($scope, feedbackService, $filter) {
  // Constructor for this controller
  init();

  function init() {
      feedbackService.getFeedbackPaged().then(function(data){
          $scope.feedbackItems=data;
      });
  }
});

Upvotes: 1

kaz9n
kaz9n

Reputation: 1

Use $http service as follows.

$http.get(
  'http://domain/api/something'
).then(function successCallback(response) {
  $scope.data = JSON.parse(response.data);
}, function errorCallback(response) {
  // error handler
});

reference: https://docs.angularjs.org/api/ng/service/$http

Upvotes: 0

Related Questions