AngularJS - cannot create property method on string

梦想的初衷 提交于 2020-01-06 03:22:10

问题


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.
});


来源:https://stackoverflow.com/questions/36258403/angularjs-cannot-create-property-method-on-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!