0

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

Shubham
  • 189
  • 9

1 Answers1

0

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);
});
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • If you want help, you need to be more precise than "not working". What do you expect to happen, and what happens instead, precisely? – JB Nizet Apr 07 '16 at 06:39
  • thanks its working fine. i wasn't treating data as array that was the probblrm – Shubham Apr 07 '16 at 09:19