简体   繁体   English

AngularJS - 在POST请求的主体中发送数据

[英]AngularJS - Send data in body of POST request

I am trying to send data in the body of a POST request to a RESTful API in my AngularJS application. 我正在尝试将POST请求正文中的数据发送到我的AngularJS应用程序中的RESTful API。

My current structure for this request is a controller which grabs data and send this to a service , which then calls the request from its associated factory , as follows: 我对此请求的当前结构是一个控制器 ,它抓取数据并将其发送到服务 ,然后服务从其关联的工厂调用请求,如下所示:

Controller 调节器

userService.saveReport(vm.user.id, searchParams)

Service 服务

service.saveReport = function (id, searchData) {
      var deferred = $q.defer();

      user.saveReport({ userID: id, searchData: searchData }, function(response) {
        deferred.resolve(response);
      }, function(e) {
        deferred.reject(e);
      });

      return deferred.promise;
    };

Factory

    function user($resource, $localStorage, constants) {
    return $resource(constants.API_URL + '/users', { userID: '@userID' }, {
      getReports: {
        method: 'GET',
        url: constants.API_URL + '/users/:userID/reports',
        format: 'json',
        headers: {
          'Accept': 'application/vnd.abp.v1+json',
          'X-Mode': function() {
            return $localStorage.get('mode');
          }
        }
      },
      saveReport: {
        method: 'POST',
        url: constants.API_URL + '/users/:userID/reports',
        format: 'json',
        headers: {
          'Accept': 'application/vnd.abp.v1+json',
          'X-Mode': function() {
            return $localStorage.get('mode');
          }
        }
      }
    });
  }

  angular
    .module('abp')
    .factory('user', user);

})();

I have seen various posts which state $resource allows data: to be send through to form the body of the request, but this doesn't seem to be working. 我已经看到各种帖子,状态$resource允许data:通过发送来形成请求的主体,但这似乎不起作用。 I've tried to add searchData: '@searchData' into the params allowed by the $resource call and then add this into a data section as follows, but this doesn't work (condensed for brevity): 我试图将searchData: '@searchData'添加到$resource调用允许的参数中,然后将其添加到数据部分中,如下所示,但这不起作用(为简洁起见):

return $resource(constants.API_URL + '/users', { userID: '@userID', searchData: '@searchData' }, {
    saveReport: {
        method: 'POST',
        url: constants.API_URL + '/users/:userID/reports',
        format: 'json',
        data: ':searchData',
        headers: {
          'Accept': 'application/vnd.abp.v1+json',
          'X-Mode': function() {
            return $localStorage.get('mode');
          }
        }
      }
}

How can I send the data of this request in the body of the POST request? 如何在POST请求的正文中发送此请求的数据?

OK, so I've managed to get this working now by changing how I'm sending the searchData in the service . 好的,所以我已经设法通过改变我在服务中发送searchData来实现这一点。 My controller and factory remain unchanged, however I have changed the service to the following, which seems to work absolutely perfectly: 我的控制器工厂保持不变,但我已将服务更改为以下内容,这看起来非常完美:

service.saveReport = function (id, searchData) {
      var deferred = $q.defer();

      user.saveReport({ userID: id }, searchData, function(response) {
        deferred.resolve(response);
      }, function(e) {
        deferred.reject(e);
      });

      return deferred.promise;
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM