I have a mock server that respond with some data
backendMock.run(function($httpBackend){
$httpBackend.whenGET('views/login.html').passThrough();
$httpBackend.whenGET('views/home.html').passThrough();
$httpBackend.whenGET('views/quote.html').passThrough();
var quotes = [{quote:'aint bout how hard you hit'}];
$httpBackend.whenGET('/quotes').respond(quotes);
});
to fetch the data from this mock server I am using $http service
app.controller("quoteCtrl",['$scope','$stateParams','$http',function($scope,$stateParam,$http){
$scope.myquote=$stateParam.id;
$http.get('/quotes').success(function(data){
alert(data.quote);
});
}]);
Problem is I am able to hit the server but I am not getting any data back
quotes
is not defined yet at the time $httpBackend.whenGET('/quotes').respond(quotes);
is executed. Define it before.
Also, your code displays data.quotes
, but data
is [{quote:'aint bout how hard you hit'}];
. So it's not an object, but an array containing a single element, and this element is an object with an attribute named quote
, not quotes
.
So the code should rather be
alert(data[0].quote);
You also shouldn't use success()
: it's deprecated. Use then()
:
$http.get('/quotes').then(function(response){
alert(response.data[0].quote);
});