AngularJS - cannot create property method on strin

2019-07-25 11:08发布

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条回答
男人必须洒脱
2楼-- · 2019-07-25 11:48

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.
});
查看更多
登录 后发表回答