PUT请求通过URL发送params

jas*_*328 5 http put params angularjs angular-resource

我有一个看起来像这样的订单资源.

.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'},
  });
}
Run Code Online (Sandbox Code Playgroud)

我这样跑的Order.update时候

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);
})
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

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

发送到服务器的params最终位于URL内.例如,这是服务器收到的网址之一

path="/api/mobile/orders.json?order=%7B%22line_items_attributes%22:%7B%220%22:%7B%22quantity%22:8,%22id%22:356265%7D%7D%7D"
Run Code Online (Sandbox Code Playgroud)

我还应该注意,服务器接收的方法是一个OPTIONS请求.服务器已设置为处理此问题.

由于我发送PUT请求,为什么$resource通过URL而不是有效负载的一部分传递参数?

Gio*_*cia 1

来自文档

非 GET“类”操作:Resource.action([参数], postData, [成功], [错误])

有效负载是第二个参数,所以尝试使用此代码

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);
})
Run Code Online (Sandbox Code Playgroud)

只需添加一个空对象作为更新方法的第一个参数。

另请参阅与自定义放置请求相关的部分