使用 $resource 来 POST 正文

cus*_*ice 1 angularjs angular-resource

如何使用POSTAngular 对有效负载主体执行法线操作$resource。现在,当我 时POST,它会发布到/api/example?name=JoeSmith&is_whatever=false,而不是通过正文发布。

假设我有以下内容:

ENDPOINT: `/api/example`
BODY: {
   "name": "Joe Smith",
   "is_whatever": false
}
Run Code Online (Sandbox Code Playgroud)

接口服务

angular.module('example')
   .factory('APIService', ['$resource',

        function($resource) {

           return $resource('/api/example', {}, {
              create: {
                 method: 'POST',
              }
           });          

        }]);
Run Code Online (Sandbox Code Playgroud)

用法示例

    // body i need to POST
    var payload = {
       name: 'Joe Smith',
       is_whatever: false        
    };

    APIService.create(payload).$promise.then(function(res){
        // doesnt work
    });
Run Code Online (Sandbox Code Playgroud)

gas*_*ini 5

尝试将数据参数传递给资源的操作方法,如下所示:

angular.module('example', ['ngResource'])

.run(function(APIService) {
   var payload = {
      name: 'Joe Smith',
      is_whatever: false        
   };
   APIService.save({}, payload)
})

.factory('APIService', function($resource) {
   return $resource('/api/example');
});
Run Code Online (Sandbox Code Playgroud)