如何不以请求主体的角度发送URL模板参数? [英] How do I not send url template parameters with request body in angular?

查看:93
本文介绍了如何不以请求主体的角度发送URL模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我设置了这样的资源:

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'});

有什么办法可以防止typeid作为请求正文的一部分提交,而只是在PUT有效负载中发送name吗?我无法控制要提交的API,而且似乎不喜欢我要发送的额外参数.

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.

谢谢!

更新-13/10/25-13:38

Update - 10/25/13 - 13:38

资源的文档说:

如果参数值以@开头,则将从数据对象中提取该参数的值(对非GET操作有用).

If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object (useful for non-GET operations).

这意味着应该从数据中删除参数:

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.

推荐答案

FWIW,由于有了@ Reboog711,我确实找到了一种解决方法,方法是包含如下所示的transformRequest参数:

FWIW, I did find a workaround, thanks to @Reboog711, by including a transformRequest parameter like so:

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'}
    }}
);

这篇关于如何不以请求主体的角度发送URL模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆