简体   繁体   中英

AngularJS badcfg on 'GET' request

I've got an API that returns data to AngularJS based on a given ID, however, when the data is returned as JSON, AngularJS is throwing a 'badcfg' error, which I understand to be due to the format of the returned data, but can't figure out how to fix it...

Factory:

app.factory('Companies', function($resource) {
    return $resource('/api.php/companies/:id', {id:'@id'}, {
        'query': {method: 'GET', isArray: true},
        'save': {
            method: 'POST',
            isArray: true,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            transformRequest: function(obj) {
            var str = [];
            for(var p in obj)
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
            return str.join("&");
        }},
        'delete': {method: 'DELETE'}
    });

});

Controller:

app.controller("ViewCompaniesController", ['$scope', 'Companies', '$routeParams', function($scope, Companies, $routeParams) {
    $scope.title = 'View';

    var companyId = $routeParams.id;

    var theCompany = Companies.get({ id: companyId }, function() {
        console.log(theCompany);
    });
}]);

Error:

Error in resource configuration for action Error in resource configuration for action {0} . Expected response to contain an {1} but got an {2} (Request: {3} {4}) . Expected response to contain an {1} but got an {2} (Request: {3} {4}) . Expected response to contain an get but got an object (Request: array GET)

Could anybody help resolve this?

according to the ng-doc you're not useing your defined request:

 var theCompany = Companies.query({ id: companyId }, function() {
        console.log(theCompany);
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM