Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to use AngularJS $resource to DELETE data from my server. However, when I write the result to console, I do not see the data.

However, when I go to "Network" in Chrome's console, I see the DELETE in the Name Path left column. When I click on the "info, I see five tabs on the right panel. Under the Preview and Response tabs, I see the correct data. I just don't know how to see or retrieve that in my Javascript.

Here is the javascript service code:

var MyServices = angular.module('MyServicesName', ['ngResource']);

MyServices.factory('AAAService', function($resource) {
    return $resource(serverBaseUrl + 'users/:userId/Video/:videoId/', {userId: '@userId', videoId: '@videoId'}, {
        show: {method: 'GET'},
        update: {method: 'PUT', params: {id: '@id'}},
        delete: {method: 'DELETE', isArray:false}
    });
});

And the Controller:

quizcatControllers.controller('BBBCtrl', ['$scope', '$stateParams', '$http', 'AAAService',
    function($scope, $stateParams, $http, AAAService) {
$scope.deleteQuestion = function(user, videoId) {
             AAAService.delete({userId: user, videoId: videoId}, function(a, b) {
        console.log(a);//Expect to print the data
        console.log(b);
});

Can someone suggest how my code should be changed so that I can fetch the data from the response? Although my response data is not in array format I would like to know how to do it for both: array and not array style.

What should be the proper names instead of a and b on the following line:

AAAService.delete({userId: user, videoId: videoId}, function(a, b) {

UPDATE:

This is the result I get in the success callback for the parameter returnValue:

0: "S"
1: "u"
2: "c"
3: "e"
4: "s"
5: "s"
$promise: Object
$resolved: true
proto: Resource

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Using resources can be a bit confusing (at least they originally were for me).

I think your problem is that your call to delete isn't using the correct signature (there are different signatures for 'GET' methods and 'non-GET' methods. In this case, it looks like you are sending what is intended to be your callback function (for success and error) as postData. The signature you used is for 'GET' methods (which doesn't have postData).

The signature for your call to delete should look like this (see documentation here):

Resource.action([parameters], postData, [success], [error])

So, you can do something like this:

quizcatControllers.controller('BBBCtrl', ['$scope', '$stateParams', '$http', 'AAAService', 
function($scope, $stateParams, $http, AAAService) {
    $scope.deleteQuestion = function(user, videoId) {
        AAAService.delete(
            {userId: user, videoId: videoId},  // parameters
            {},                                // postData, which you don't need for this
            // success callback
            function (returnValue, responseHeaders) {
                // do what you want with the returnValue from the call here
            },
            // error callback
            function (httpResponse) {
                // do what you want for error handling here
            })
    };
}]);
share|improve this answer
    
Thanks, this fixes a bit my code. The problem is that the value in return value is of type Resource. How can I get the real value(a string)? –  Randall Flagg May 21 '14 at 13:11
    
Have you tried AAAService.delete(...).then(function(data) { console.debug(data) })? –  link May 21 '14 at 13:26
    
@link yes. I am getting the result shown in the UPDATE –  Randall Flagg May 21 '14 at 13:40
    
Strange, you should be getting only the data, while you seem to be getting the whole resource. –  link May 21 '14 at 14:06
    
What are you using for your API? I suspect that you are returning just a string, rather than an object (JSON). For example, if your API just returns the string "Success", I think that it is being serialized (or just deserialized) into an array of characters, which is what you see in your result. Instead, you should generally return objects, such as "{ 'result': 'success' }". Then in your success callback access returnValue.result. Getting the $promise and $resolved is normal, but you don't need to worry about them. –  RobM May 21 '14 at 15:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.