AngularJS - cannot create property method on strin

2019-07-25 11:50发布

问题:

I am sending a normal get request as follows which was working normally:

service.show = function (slug) {
            console.log(slug)
            return $http.get('api/packages/'+slug, slug).success(function(response){
                service.package = response.package;
            });
        };

Suddenly after I started combining all js files together using gulp I get this weird error:

angular.js:10147 TypeError: Cannot create property 'method' on string 'package-38'

Since the application is an api so I tried to run the request using postman to see if it's working and it was. However when the same request is called in a certain page, I get this error.

I check variable slug which is a string and it is sent normally, so what could be the possible bug here?!

回答1:

The second parameter $http.get expects, is an object with http request configuration.

Have a look at angular documentation. See the Shortcut methods section.

$http.get('/someUrl', config).then(successCallback, errorCallback);

Or better and readable call looks like below:

$http({
  method: 'GET',
  url: 'api/packages/'+slug
}).then(function(response){
            service.package = response.package;
        }, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});