0

I want to load json from a file, driverStandings.json (you see it left in the structure from the image). I use a service API to load this data in. I use following code(right in the image).

Left the project sturcture, right the service class

When I compile, I don't get my drivers list, I get following error list

TypeError: url.replace is not a function
    at angular.js:8557
    at sendReq (angular.js:8426)
    at $get.serverRequest (angular.js:8146)
    at deferred.promise.then.wrappedCallback (angular.js:11682)
    at deferred.promise.then.wrappedCallback (angular.js:11682)
    at angular.js:11768
    at Scope.$get.Scope.$eval (angular.js:12811)
    at Scope.$get.Scope.$digest (angular.js:12623)
    at Scope.$get.Scope.$apply (angular.js:12915)
    at done (angular.js:8450)

What is a TypeError and what am I doing wrong?

1
  • Please post live code not images. Makes it very hard to copy to help create answers Commented Aug 22, 2015 at 21:02

3 Answers 3

1

You don't want to use jsonp as method for a static json resource and you are nesting another $http call as the url to the outer ones

Just use the inner $http and get rid of the outer ones

FAPI.getDrivers = function(id){
    return $http.get( ... path to json file ...)
}
Sign up to request clarification or add additional context in comments.

3 Comments

You mean: F1API.getDrivers = function() { return $http.get('/app/data/driverStandings.json'); } ??
Yes...exactly what I mean
Perfect, you derserve a big heart (L)
1
angular
    .module('FomulaOne.services', {})
    .factory('F1APIService', function ($http)
        {
            function http_get (url, doneFunc, failFunc)
            {
                var promise = $http.get(url);

                promise.success(doneFunc);

                if ( failFunc ) // optional error catch
                    promise.error( failFunc );

                return promise;
            }

            return {
                getDrivers: function (doneFunc, failFunc)
                {
                    return http_get( '/app/data/driversStandings.json', doneFunc, failFunc );
                },
                getDriverDetails: function (id, doneFunc, failFunc)
                {
                    return http_get( '/app/data/' + id + '/driversStandings.json', doneFunc, failFunc );
                },
                getDriverRaces: function (id, doneFunc, failFunc)
                {
                    return http_get( '/app/data/' + id + '/results.json', doneFunc, failFunc );
                }
            };
        });

To use

F1APIService.getDriverDetails(
    123,
    function (response)
    {
        // Do stuff on success
    },
    function (response)
    {
        // Do stuff on error
    }
);

Comments

0

As you can see here in the documentation, url requires an string with a proper url, just remove the $http.get from there and it should work, but first take a look at the doc.

Comments

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.