set request body and params on $resource POST

原创
2018/08/30 14:25
阅读数 62

I'm attempting to pass data in the request body and a request parameter to an Angular $resource call. Below is my click handler from my controller and the service which it calls:

controller.js:

vm.setLimit = function(limit) {
  var data = {
    activity: 'point_limit',
    limit: limit
  };
  playersService.setPlayerLimit({
    playerId: playerId,
    data
  });
};

service.js:

angular.module('gameApp')
  .factory('playersService', ['$resource',
    function($resource) {
      var base = '/api/players/:playerId/';
      return $resource(base, {}, {
        getPlayerInfo: {method: 'GET', url: base + 'playerInfo'},
        setPlayerLimit: {method: 'POST', url: base + 'playerLimit'}
      });
    }]);

getPlayerInfo works, but setPlayerLimit does not because, for some reason, it is not being passed the playerId.

Answer:

playersService.setPlayerLimit should take 4 parameters in order:

(request parameters, request body, success callback, error callback)

Modify your code as:

playersService.setPlayerLimit({playerId: playerId}, data, success, error);
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部