我正在向AngularJS资源添加一对操作,但是当我调用该操作时,我的transformRequest函数没有被调用:
var _resource = $resource('api/NewItem/:id',
{ id: '@id' },
{
create: {
method: 'POST',
transformRequest: function (data, headersGetter) {
var result = JSON.stringify(data.productIntro);
return result;
}
},
update: {
method: 'PUT',
transformRequest: function (data, headersGetter) {
var result = JSON.stringify(data.productIntro);
return result;
}
}
});
如果我在应用程序上全局添加该函数,它可以工作:
var newItemApp = angular.module('newItemApp', ['ngResource'])
.config(function ($httpProvider) {
$httpProvider.defaults.transformRequest = function(data)
{
if (data === undefined) {
return data;
}
var result = JSON.stringify(data.productIntro);
return result;
};
});
我需要做的是从任何POST或PUT操作中删除根元素,因为当json对象具有命名根时,Web中的默认模型绑定不会绑定该对象。
发布于 2013-08-29 14:47:37
从AngularJS 1.1.2开始支持transformRequest
。如果您使用早期版本,则需要将其添加到$httpProvider
中。
https://stackoverflow.com/questions/18513004
复制相似问题