angularjs – PUT请求通过URL发送params

前端之家收集整理的这篇文章主要介绍了angularjs – PUT请求通过URL发送params前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个看起来像这样的订单资源.

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

当我像这样运行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);
})

我也试过这个:

Order.update({},params,resp);
})

发送到服务器的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"

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

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

解决方法

docs

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

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

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

Order.update({},resp);
})

只需将一个空对象作为第一个参数添加到update方法中.

还看看他与custom put requests相关的部分

猜你在找的Angularjs相关文章