Suppose I have a resource set up like this:
假设我有一个这样的资源设置:
resource = $resource(
"http://foo.com/service/:type/:id",
{},
{save: {method:'PUT', params: {type:'@type', id: '@id'}}}
);
resource.save({type:'user', id:14, name:'Bob Dole'});
Is there any way I can prevent type
and id
from being submitted as part of the request body, and just send name
in the PUT payload? I don't control the API I am submitting to, and it seems to not like the extra parameters I am sending it.
有什么办法可以阻止类型和id作为请求体的一部分提交,只是在PUT有效负载中发送名称?我不控制我提交的API,它似乎不喜欢我发送它的额外参数。
Thanks!
谢谢!
Update - 10/25/13 - 13:38
更新 - 2013年10月25日 - 13:38
The documentation for resource says this:
资源文档说:
If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object (useful for non-GET operations).
如果参数值以@为前缀,则从数据对象中提取该参数的值(对非GET操作很有用)。
That implies that this ought to remove the parameters from the data:
这意味着这应该从数据中删除参数:
resource.save({type:'@user', id:'@14', name:'Bob Dole'});
but it doesn't seem to work. Still at a loss.
但它似乎没有用。还是不知所措。
2 个解决方案
#1
33
Use the first parameter for your url template parameters and put your post data in the second parameter like this:
使用第一个参数作为您的网址模板参数,并将您的发布数据放在第二个参数中,如下所示:
resource.save({id:14, type:'user'}, {name:'Bob Dole'});
Here's the line from the Angular docs that shows the function signature:
以下是Angular文档中显示函数签名的行:
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
非GET“类”动作:Resource.action([parameters],postData,[success],[error])
Here's an example in plunker
这是一个关于plunker的例子
The request you get does not have the url parameters in the body:
您获得的请求在正文中没有url参数:
Request URL:http://run.plnkr.co/JAOqZqW6RSywatUM/badUrl/user/14
Request Method:PUT
Request Payloadview source
{name:Bob Dole}
#2
7
FWIW, I did find a workaround, thanks to @Reboog711, by including a transformRequest parameter like so:
FWIW,我确实找到了一个解决方法,感谢@ Reboog711,通过包含一个transformRequest参数,如下所示:
resource = $resource(
"http://foo.com/service/:type/:id",
{},
{save: {
method:'PUT',
transformRequest:function(data) {
delete data.type;
delete data.id;
return JSON.stringify(data);
},
params: {type:'@type', id: '@id'}
}}
);
#1
33
Use the first parameter for your url template parameters and put your post data in the second parameter like this:
使用第一个参数作为您的网址模板参数,并将您的发布数据放在第二个参数中,如下所示:
resource.save({id:14, type:'user'}, {name:'Bob Dole'});
Here's the line from the Angular docs that shows the function signature:
以下是Angular文档中显示函数签名的行:
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
非GET“类”动作:Resource.action([parameters],postData,[success],[error])
Here's an example in plunker
这是一个关于plunker的例子
The request you get does not have the url parameters in the body:
您获得的请求在正文中没有url参数:
Request URL:http://run.plnkr.co/JAOqZqW6RSywatUM/badUrl/user/14
Request Method:PUT
Request Payloadview source
{name:Bob Dole}
#2
7
FWIW, I did find a workaround, thanks to @Reboog711, by including a transformRequest parameter like so:
FWIW,我确实找到了一个解决方法,感谢@ Reboog711,通过包含一个transformRequest参数,如下所示:
resource = $resource(
"http://foo.com/service/:type/:id",
{},
{save: {
method:'PUT',
transformRequest:function(data) {
delete data.type;
delete data.id;
return JSON.stringify(data);
},
params: {type:'@type', id: '@id'}
}}
);