Angularjs $ resource DELETE不在请求正文中传递数据

时间:2013-09-11 23:00:45

标签: angularjs

我有以下代码:

deleteArticle = function(assetData) {
   var defer;
   var assetId = assetData.assetId;
   var cseService = $resource(CSE_CONFIG.apiBaseUrl + 'articles/' + assetId,{},   {'remove': {method: 'DELETE', isArray: false}});

   defer = $q.defer();
   $log.info(assetData.data);
   cseService.remove(assetData.data, function(results) {
       return defer.resolve(results);
   }, function(results) {
      $log.error('suggestionsService deleteArticle error', results);
      return defer.reject(results);
   });

   return defer.promise;
}

assetData = {“assetId”:12345,“data”:{“不当”:true,“评论”:“这是评论”}}

我使用express作为服务器,这是我的路线:

app.delete('/api/v1/articles/:assetId', function (req, res) {
        console.log("delete is called for " + req.params.assetId);
        console.log(req.body);
        for (var i = 0; i < suggestions[0].articles.length; i++) {
            var row = suggestions[0].articles[i];
            if (row.assetId === req.params.assetId) {
                suggestions[0].articles.splice(i,1);
                console.log("REMOVED:::::");
                console.log(row);
                return res.send("OK",200);
            }
        }
        return res.send("BAD Request",400);
    });

当我将其发送到服务器时,req.body不包含assetData.data。所以问题是,如何使用method = DELETE使用$ resource发送正文? 注意:我使用Chrome:Postman REST客户端测试了服务器,它正常工作,但在使用角度时却没有。

1 个答案:

答案 0 :(得分:2)

您可以修改$ resource的每个请求的默认行为。 检查$ resource tranformRequest

示例:

var myResource = $resource(url, params, {
   'delete' : function(data,headers) {
       var myData = { bob: 'bob' };
       return myData;
   }
}