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?
-
Please be more descriptive about what you're trying to accomplish, and also make some attempt at doing so.Lucas Watson– Lucas Watson2016-07-22 04:28:58 +00:00Commented Jul 22, 2016 at 4:28
-
Assume that user enter my page "domain/api/something" on browser then my server returns a JSON data/ a Html page or so. In case of a Html page, the browser will render it. The question is, how can I also get that Html page in text/HTML format without sending another request.Vinh.TV– Vinh.TV2016-07-22 04:59:24 +00:00Commented Jul 22, 2016 at 4:59
Add a comment
|
2 Answers
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;
});
}
});
2 Comments
Vinh.TV
By this way, I'll send another request rather than catch a response that already sent by Browser, isn't it?
Jaldhi Oza
Please give more descriptive. once you load the init(); It will call the http GET method and response will store in $scope.feedbackItems. Thanks
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
});