简体   繁体   中英

web services parameters in angularjs? can't find solution

I've already tried the other solutions on stackoverflow with no success.

app.services.factory('SearchResource', ['$resource', function ($resource) {
    return $resource("https://..../rest/demo/v1/AMID4?auth=demo_full:1", {}, {'query':     {method: 'GET', isArray: false}}); //I've also tried true...        
}]);



app.services.factory('SearchService', ['$q', 'SearchResource', function($q, SearchResource) {
return {
    getAllSearch: function() {
        var delay = $q.defer();
        SearchResource.get(
            function (response) {
                console.log(JSON.stringify(response));
                delay.resolve(response);
            },
            function (response) {
                delay.reject("can't get search source");
            });
        return delay.promise;
    }
}
}]);

I'm using the above code but I get the following error msg:

Error: [$resource:badcfg] Error in resource configuration. Expected response to contain an object but got an array

Now I've tried the solutions to all the other posts and I still can't figure it out... when I go to network tab --> response (in inspsect element) I can see the result, It must have something to do with the parameters but I'm pretty sure I've tried most formats at this stage... I'm relatively new to angular, am I missing something!!

Try this :

app.services.factory('SearchResource', [
  '$resource', function ($resource) {

return $resource("https://..../rest/demo/v1/AMID4?auth=demo_full:1", {}, {
  query: { method: 'GET', isArray: true}}); 
}]);

Then you are using SearchResource.get in your code when you should use SearchResource.query , or you can just name it get .

You have to define the actions your Resource offer and then use it, as documented here (check bottom of the page for an example).

Also, make sure you don't miss any headers in your $resource definition.

Finally, I prefer to use this (in my opinion) cleaner call:

SearchResource.query({
  param_id: stuff
}).$promise.then(function(data) {
  return $scope.thing = data;
}, function(e) {
  return console.log(e);
});

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