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. 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):
我已经看到各种帖子,状态$ resource允许数据:通过发送来形成请求的主体,但这似乎不起作用。我试图将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请求的正文中发送此请求的数据?
1 个解决方案
#1
OK, so I've managed to get this working now by changing how I'm sending the searchData
in the service. My controller and factory remain unchanged, however I have changed the service to the following, which seems to work absolutely perfectly:
好的,所以我已经设法通过改变我在服务中发送searchData的方式来实现这一点。我的控制器和工厂保持不变,但我已将服务更改为以下内容,这看起来非常完美:
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;
};
#1
OK, so I've managed to get this working now by changing how I'm sending the searchData
in the service. My controller and factory remain unchanged, however I have changed the service to the following, which seems to work absolutely perfectly:
好的,所以我已经设法通过改变我在服务中发送searchData的方式来实现这一点。我的控制器和工厂保持不变,但我已将服务更改为以下内容,这看起来非常完美:
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;
};