无法读取AngularJS $资源响应DELETE资源、AngularJS、DELETE

2023-09-14 00:16:44 作者:其实我不笨只不过懒得聪明

我想使用AngularJS $资源,从我的服务器删除数据。然而,当我写的结果来安慰,我没有看到的数据。

然而,当我进入网络在Chrome的控制台,我看到名称路径左列中删除。当我点击信息,我看到右侧面板上五个选项卡,根据preVIEW和响应选项卡上,我看到了正确的数据。我只是不知道怎么看或检索,在我的javascript。

下面是JavaScript服务code:

  VAR MyServices = angular.module('MyServicesName',['ngResource']);MyServices.factory('AAAService',函数($资源){    返回$资源(serverBaseUrl +用户/:用户名/视频/:VIDEOID /',{用户名:@userId',VIDEOID:@videoId'},{        显示:{方法:GET},        更新:{方法:把',则params:{ID:'@id'}},        删除:{方法:删除,IsArray的:假}    });}); 

和控制器:

  quizcatControllers.controller('BBBCtrl',['$范围,$ stateParams,$ HTTP,AAAService',    功能($范围,$ stateParams,$ HTTP,AAAService){$ scope.deleteQuestion =功能(用户,VIDEOID){             AAAService.delete({用户名:用​​户,VIDEOID:VIDEOID}功能(A,B){        的console.log(一); //期望打印的数据        的console.log(B);}); 
AngularJS快速入门

可有人建议我的code应如何改变这样我可以从获取响应的数据?虽然我的响应数据是不是在阵列格式,我想知道如何做到这一点为:阵列和阵列没有风格

应该是什么正确的名称,而不是A和B上以下行:

  

AAAService.delete({用户名:用​​户,VIDEOID:VIDEOID}功能(A,B){

更新:

这是结果我成功的回调参数的returnValue得到:

  

0:S的结果,     1:U结果     2:C结果     3:E结果     4:S结果     5:S搜索     $承诺:对象结果  $解决:真正的结果  原型:资源

解决方案

使用的资源可以是一个有点混乱(至少他们最初对我来说)。

我觉得你的问题是,你的电话,删除未使用正确的签名(有对GET方法和非GET'方法。在这种情况下,不同的签名,它看起来像您发送的是旨在成为你的回调函数(成功和错误)作为POSTDATA。你所使用的签名是GET方法(没有POSTDATA)。

你的电话中删除看起来应该像这样(见文档的这里):

  

Resource.action([参数],POSTDATA,[成功],[错误])

所以,你可以做这样的事情:

  quizcatControllers.controller('BBBCtrl',['$范围,$ stateParams,$ HTTP,AAAService',功能($范围,$ stateParams,$ HTTP,AAAService){    $ scope.deleteQuestion =功能(用户,VIDEOID){        AAAService.delete(            {用户名:用​​户,VIDEOID:VIDEOID} //参数            {} // POSTDATA,你不需要为这            //成功回调            功能(的returnValue,responseHeaders响应){                //你想要做什么用从这里调用的returnValue            },            //错误回调            功能(HTT presponse){                //你想要做什么的错误处理这里            })    };}]); 

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

解决方案

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