0

AngularJS : 1.4.X
Scenario 1: Works fine
Scenario 2: Throws 404 error on line xhr.send(isUndefined(post) ? null : post); in angular.js

I'm trying to add inbuilt angular cache to our existing app, As i mentioned in scenario 1 we consume rest call in factory 'restFactory' and inject the factory to controller 'bookController', the promise is resolved and data loads fine.

Factory: restFactory

(function () {
    "use strict";
    angular.module('myApp').factory("restFactory",['$http','confi', function($http,config){

    function getBooks (){  
       return $http.get(config.serverName+"bookshelf/rest/books/data/geRestBooks");
    }
    return {
        getBooks : getBooks
    };
}]);
})();

Controller: bookController

   $scope.getComicbooks = function() {
        restFactory.getBooks().then(function(response) {        
            $scope.names = response.data;
        }, function(error) {
            $scope.error = error;
            $scope.names = [];
        });
    };

Now in scenario 2, I changed the service call in factory to object with more details. But i get exception from controller while resolving the promise ( i have only added the changed code )

function getBooks (){  
   return $http.get({
   cache: true,
   method: 'GET',
   url : config.serverName+"bookshelf/rest/books/data/geRestBooks"
   });
}

ERROR: angular.js:10765 GET http://127.0.0.1:53814/views/[object%20Object] 404 (Not Found)

Network Tab:
enter image description here In scenario#1 this would have been a method call getBooks

8
  • 1
    Where do you call 127.0.0.1:53814/views ? Commented Jan 23, 2017 at 21:58
  • @AdamWolski: I consume bookController in index page which is in views directory, but i when i added console.log it breaks before it resolves restFactory.getBooks().then(function(response) {. Commented Jan 23, 2017 at 22:01
  • What do you see in the network tab for that request? Commented Jan 23, 2017 at 22:08
  • @AmyBlankenship: updated Commented Jan 23, 2017 at 22:16
  • 2
    your urls don't even match here. the first example has config.serverName+"bookshelf/rest/books/data/geRestBooks", while the second has config.serverName+"bookshelf/rest/books/data/getBooks" Commented Jan 23, 2017 at 22:23

1 Answer 1

3

If you are going to specify the method, then you should just use the $http constructor method, not the get method.

Instead of

function getBooks (){  
   return $http.get({
      cache: true,
      method: 'GET',
      url : config.serverName+"bookshelf/rest/books/data/geRestBooks"
   });
}

Try

function getBooks (){  
   return $http({
      cache: true,
      method: 'GET',
      url : config.serverName+"bookshelf/rest/books/data/geRestBooks"
   });
}
Sign up to request clarification or add additional context in comments.

4 Comments

Excellent catch! The reason OP's code fails is because $http.get expects the first argument to be a URL string. Since OP is passing an object, they get the string version of an object, ie [object Object]
Alternatively, OP could use $http.get(config.serverName+"bookshelf/rest/books/data/geRestBooks", { cache: true })
@Phil - I actually prefer your suggestion and was going to mention it, but figured I should stick to helping resolve the issue since I've been penalized for suggesting alternatives before.
It's hardly an alternative. Internally, $http.get just proxies through to $http with method pre-set and url added from the first argument. Feel free to add it to your answer :)

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.