带有效负载的角度资源PUT操作

时间:2016-07-12 10:20:30

标签: angularjs rest put angular-resource payload

我有以下工厂向服务器发送查询:

app.factory('Request', ['$resource',
    function ($resource) {
        var res = $resource("bin/server.fcgi/REST/" + ':resourceName/:ID', {}, {
                get : {
                    method : 'GET'
                },
                put : {
                    method : "PUT"
                }
            });
        return {
            get : function (arguments, b, c) {
                return res.get(arguments, b, c).$promise;
            },
            put : function(arguments,b,c){
                return res.put(arguments, b, c).$promise;
            }
        };
    }
]);

我称之为:

Request[methodName](params).then(successFunction).catch (failFunction);

但是,如果我想发送PUT查询:

Request["put"](params).then(successFunction).catch (failFunction);

,其中

params = {
   resourceName : "ATable",
   ID : 222,
   AProperty : "changedValue"
}

然后我接受以下请求:(所以出错)

http://myadres.com/REST/ATable/222?AProperty=changedValue

而不是

http://myadres.com/REST/ATable/222

有效载荷

{ AProperty:changedValue }

这有什么问题?

1 个答案:

答案 0 :(得分:1)

app.service('Request', ['$resource',function ($resource) {
        var res = $resource('bin/server.fcgi/REST/:resourceName/:ID', 
            {resourceName: "@resourceName", ID: "@ID"}, 
            {
                get : { method : 'GET'},
                put : { method : "PUT", params: {resourceName:"@resourceName", ID: "@ID"}//you can leave the string empty if you dont want it to be a defualt value like ID:""
            });

        this.get = function () {
            return res.get().$promise;
        }
        this.put = function(obj){
            return res.put(obj).$promise; // it can be also {like json with your params}
        }
]);

然后通过

从控制器调用它
var obj = {
    ID:222,
    resourceName:'ATable'
}
Request.put(obj).then(function(data){
    //check whats the data
})

这是应该怎么做的 也许不是最好的方法,但应该工作