我有一个看起来像这样的订单资源。
.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, function (resp, respHeaders) {
console.log("response headers", respHeaders());
console.log("change quantity resp", resp);
})
发送到服务器的参数最终位于 URL 中。例如,这是服务器收到的 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 传递参数而不是负载的一部分?
最佳答案
来自 docs :
non-GET "class" actions: Resource.action([parameters], postData, [success], [error])
有效载荷是第二个参数,所以用这段代码试试
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);
})
只需添加一个空对象作为更新方法的第一个参数。
另请查看与 custom put requests 相关的部分
关于angularjs - PUT 请求通过 URL 发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32103125/