如何在angularjs DELETE请求中将数据作为对象发送

时间:2016-01-21 12:01:19

标签: javascript angularjs node.js

我正在尝试使用angularjs $ resource delete方法将元素的ID发送到nodejs api,该值是作为参数值发送的,我该如何将该值作为body参数发送?

我的代码就像这样

在angularjs中

    var ids = {
        delId : row._id
    }
    $resource('/deleteUser').delete(ids,function(data){
        alert(data)
    });

在nodejs中

app.delete('/deleteUser', function(req, res) {
    console.log('called');
    var resp = {
        success : true,
        id : req.body.id
        }
    console.log(req)
    res.json(resp)
});

但是它只在响应中发送成功而忽略了id值,怎么办呢?

1 个答案:

答案 0 :(得分:0)

这是我将为角部分做的事情:

  angular.module('yourModule').factory('cmsUser', function($http) {

  var cmsUser = {};

  cmsUser.Delete = function(user) {
    return $http.delete("your url/", user);
  };

  return cmsUser;
});

angular.module('yourModule').factory('userService', function(cmsUser, $q) {

  var userService = {};

  userService.Delete = function(user) {
    var deferred = $q.defer();
    cmsUser.Delete(user)
      .success(function(data, status, config, headers) {
        deferred.resolve(data);
      })
      .error(function(data, status, config, headers) {
        deferred.reject(data);
      });

    return deferred.promise;
  };

  return userService;
});

angular.module('yourModule').Controller('YourController', function(userService) {

  var vm = this;

  vm.Delete = function(user) {
    userService.Delete(user)
      .then(function() {
        // this is the sucess
        console.log("user deleted");
      }, function(reason) {
        // this is the error
        console.log(reason.message);
      });
  };
});