0

I have the following factory to send query to server:

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

I call it like this:

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

However, if i want to send a PUT query:

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

where

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

I take then following request: (so an error)

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

instead of

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

with payload

{ AProperty:changedValue }

What is wrong with this?

4
  • take a look on that question stackoverflow.com/questions/38308156/… i comment there and you should do the same- you dont need the "+"
    – Erez
    Jul 12, 2016 at 10:33
  • what do you mean exactly? I tried some versions of your answer but none of them worked. If you can give an answer, then we can discuss it on your answer.
    – Asqan
    Jul 12, 2016 at 10:51
  • show what you did try
    – Erez
    Jul 12, 2016 at 10:55
  • var res = $resource("bin/server.fcgi/REST/" + ':resourceName/:ID', {ID: 'ID'}, { put : { method : "PUT", params: { ID: "ID" } } });
    – Asqan
    Jul 12, 2016 at 10:59

1 Answer 1

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

and then call it from controller by

var obj = {
    ID:222,
    resourceName:'ATable'
}
Request.put(obj).then(function(data){
    //check whats the data
})

this is how it should be done maybe not the best way but should work

7
  • Now i take the following: http://localhost/agenda/bin/server.fcgi/REST/:@resourceName/26 its very strange
    – Asqan
    Jul 12, 2016 at 11:38
  • the names have to be equals and if its empty it can be
    – Erez
    Jul 12, 2016 at 11:40
  • the name resourceName is equal everywhere?
    – Asqan
    Jul 12, 2016 at 11:43
  • yes it should be but as you can see it works you just miss something
    – Erez
    Jul 12, 2016 at 11:46
  • yeah the query is working now with the following: $resource("bin/server.fcgi/REST/" + ':resourceName/:ID', {resourceName:"@resourceName", ID: '@ID'}, { put : { method : "PUT", params: { ID: '@ID', resourceName:"@resourceName" } } }); However resourceName is also contained in payload and it shouldn't go to server. So take now this error from server : Unknown property: "resourceName" how can we avoid that resourceName is not sent to server as a property ??
    – Asqan
    Jul 12, 2016 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.