5

I have a order resource that looks like so.

.factory('Order', order)

order.$inject = ['$resource', "ApiEndpoint"];

function order($resource, ApiEndpoint) {
  return $resource(ApiEndpoint.url + 'orders.json', {}, {
    create: {method: 'POST', url: ApiEndpoint.url + 'orders.json'},
    update: {method: 'PUT'},
    edit: {method: 'GET', url: ApiEndpoint.url + 'orders/edit.json'},
    remove_item: {method: 'GET', url: ApiEndpoint.url + 'orders/remove_item.json'},
  });
}

When I run Order.update like so

var params = {
  order: {
    line_items_attributes: {0: {quantity: 2, id: 1}}
  },
  order_id: 3
};

Order.update(params, function (resp, respHeaders) {
  console.log("response headers", respHeaders());
  console.log("change quantity resp", resp);
})

I also tried this:

Order.update({}, params, function (resp, respHeaders) {
  console.log("response headers", respHeaders());
  console.log("change quantity resp", resp);
})

The params sent to the server end up being inside the URL. For example this was one of the urls the server received

path="/api/mobile/orders.json?order=%7B%22line_items_attributes%22:%7B%220%22:%7B%22quantity%22:8,%22id%22:356265%7D%7D%7D"

I should also note that the method being received by the server is an OPTIONS request. The server has been set up to handle this.

Since I'm sending a PUT request why is $resource delivering the params via the URL and not part of the payload?

thank_you
  • 11,001
  • 19
  • 101
  • 185

2 Answers2

1

from the docs:

non-GET "class" actions: Resource.action([parameters], postData, [success], [error])

the payload is the second argument, so try with this code

var params = {
  order: {
    line_items_attributes: {0: {quantity: 2, id: 1}}
  },
  order_id: 3
};

Order.update({}, params, function (resp, respHeaders) {
  console.log("response headers", respHeaders());
  console.log("change quantity resp", resp);
})

just add an empty object as first parameter to the update method.

have also a look to he section related to custom put requests

  • I tried this and the params are still being loaded into the URL. – thank_you Aug 24 '15 at 15:34
  • the call to OPTIONS should be caused by [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing), is your api endpoint on a different domain than the angular app? – Giovanni Vinaccia Aug 24 '15 at 16:16
  • Yes it is. I believe in the past we corrected this issue as we were making a POST request in the past and the POST request does not pass params into the url like the PUT request does. – thank_you Aug 24 '15 at 16:27
  • the issue is not with PUT method but with CORS, have a look at this [codepen](http://codepen.io/anon/pen/oXKWwN), if you see the put call you'll see the parameters passed into the body of the request – Giovanni Vinaccia Aug 24 '15 at 16:42
  • Thanks. But I still don't know how to fix this. – thank_you Aug 25 '15 at 16:23
0

If you are updating an order then you should speficy the order id so that service can now which order is going to be updated

function order($resource, ApiEndpoint) {
  return $resource(ApiEndpoint.url + 'orders.json/:orderid', {}, {
    create: {method: 'POST', url: ApiEndpoint.url + 'orders.json'},
    update: {method: 'PUT',params : {orderid : '@order_id'},
    edit: {method: 'GET', url: ApiEndpoint.url + 'orders/edit.json'},
    remove_item: {method: 'GET', url: ApiEndpoint.url + 'orders/remove_item.json'},
  });
}

and then your call

Order.update(params, function (resp, respHeaders) {
  console.log("response headers", respHeaders());
  console.log("change quantity resp", resp);
})
maddygoround
  • 2,145
  • 2
  • 20
  • 32