Cannot read response from AngularJS $resource DELETE

我的梦境 提交于 2019-12-22 12:29:12

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/23749148/cannot-read-response-from-angularjs-resource-delete

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