2

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

Randall Flagg
  • 4,834
  • 9
  • 33
  • 45

1 Answers1

2

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
            })
    };
}]);
RobM
  • 3,588
  • 2
  • 19
  • 14
  • 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
  • @RobM I am using json_encode("Success") in PHP. Is that correct? – Randall Flagg May 21 '14 at 15:55
  • It is correct that it is the thing causing your problem. That is causing your string to be serialized into an array of characters. That is meant to encode an object as JSON, such as in my last comment. You shouldn't just json_encode a string. Instead you could just return a string like I put in the last comment, or json_encode an object with a property that has your result. – RobM May 21 '14 at 16:33
  • While I'm not a PHP guy, I think you could do something like this: $retObj = new stdClass; $retObj->operationResult = 'success'; then, return json_encode($retObj) in your API. Then, in Angular, access your value using returnValue.operationResult – RobM May 21 '14 at 16:37