如何在我的应用程序中发出 POST 请求
How to make POST request in my app
我正在尝试使用 $resource 来发出 post 请求。
在我的应用中,
angular.module('myApp').factory('Product', ['$resource',
function ($resource) {
return $resource( '/api/product/:id', {
id: '@id'
}, {
'save': {
method: 'POST',
isArray: true,
url: '/api/product/:id/addProduct'
//different url for adding a product.
,}
}
);
}
]);
在我的控制器中
Product.save({
name:'test name',
quantity:'5'
})
它通过url提出了请求,喜欢
/api/product/addProduct //without id
但如果我这样做
Product.save({
id:'12345',
name:'test name',
quantity:'5'
})
请求 url 与 /api/product/12345/addProduct
一样正确
但有效载荷变成
id:'12345',
name:'test name',
quantity:'5'
服务器不会将 id 参数作为负载。
我不确定如何发出 post 请求。谁能帮我解决这个问题?非常感谢!
将 'id' 对象作为保存方法中的第一个参数传递:
Product.save({"id":"12345"}, {
name:'test name',
quantity:'5'
})
我正在尝试使用 $resource 来发出 post 请求。
在我的应用中,
angular.module('myApp').factory('Product', ['$resource',
function ($resource) {
return $resource( '/api/product/:id', {
id: '@id'
}, {
'save': {
method: 'POST',
isArray: true,
url: '/api/product/:id/addProduct'
//different url for adding a product.
,}
}
);
}
]);
在我的控制器中
Product.save({
name:'test name',
quantity:'5'
})
它通过url提出了请求,喜欢
/api/product/addProduct //without id
但如果我这样做
Product.save({
id:'12345',
name:'test name',
quantity:'5'
})
请求 url 与 /api/product/12345/addProduct
一样正确
但有效载荷变成
id:'12345',
name:'test name',
quantity:'5'
服务器不会将 id 参数作为负载。 我不确定如何发出 post 请求。谁能帮我解决这个问题?非常感谢!
将 'id' 对象作为保存方法中的第一个参数传递:
Product.save({"id":"12345"}, {
name:'test name',
quantity:'5'
})