Angular.js:在使用ng-resource和transformRequest发送之前先转换数据 [英] Angularjs: Transform data before send with ng-resource and transformRequest
本文介绍了Angular.js:在使用ng-resource和transformRequest发送之前先转换数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改一些数据,然后再通过ng-resource将其发送到服务器.我这样使用tranformRequest-Function:
I want to change some data before sending it to the server via ng-resource. I use the tranformRequest-Function like this:
update: {
method: 'PUT',
transformRequest: function (data) {
// modify data then
return data;
}
}
我可以用这种方式修改数据,但是在请求中我的数据总是序列化的.我想将数据保留为JSON.这可以通过transformRequest来实现,还是可以在控制器中完成.我希望在服务中这样做.寻求帮助
I can modify data this way but in the request my data is always serialized. I want keep my data as JSON. Is this possible with transformRequest or have this to be done in controller. I would prefer to do it in the service. Thx for help
推荐答案
天哪,我觉得自己是个白痴.您只需要做
Omg i feel like an idiot. You simply have to do
update: {
method: 'PUT',
transformRequest: function (data) {
// modify data then
return angular.toJson(data);
}
}
这篇关于Angular.js:在使用ng-resource和transformRequest发送之前先转换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文