[英]AngularJs $http.get: Sending array of objects as params
我在寻找使用 AngularJS 将对象数组发送到我的 API 的正确方法时遇到问题。
前端代码
function getPrices(articles) {
return $http.get('http://someurl/api/prices/getprices', { params: { articles: articles } }).then(function (res) {
// do something with prices
}, function (err) {
// handle error
});
}
文章属于以下类型
var oneArticle = {
code: 'someCode',
quantity: 1,
stockUnit: 'piece'
}
接口代码
[VersionedRoute("getprices")]
[HttpGet]
public IHttpActionResult GetPrices([FromUri]List<Article> articles) {
// do something with input
}
文章类
public class Article {
public string Code {get;set;}
public int Quantity {get;set;}
public string StockUnit {get;set;}
}
一些问题:
1) 为什么我的 API 没有收到任何数据。 文章始终为空
2)这是正确的方法吗?
谢谢
编辑 1:使用 post 选项我在我的请求中收到以下数据,但我仍然不知道如何在 API 上处理它。
我终于让它工作了。
@Tomo:感谢您的努力
@Naimad:我很抱歉,你从一开始就是对的。
这是工作解决方案:
前端:
function getPrices(articles) {
return $http.get('http://someurl/api/prices/getprices', articles).then(function (res) {
// do something with prices
}, function (err) {
// handle error
});
}
后端
[VersionedRoute("getprices")]
[HttpPost]
public IHttpActionResult GetPrices([FromBody]List<Article> articles) {
// do something with code
}
你试过吗
return $http({
url: '/api/SomeCtrl/GetPrices',
method: 'POST',
data: JSON.stringify({ Article : articles }),
headers: { 'Content-Type': 'application/json' }
});
和
public IHttpActionResult GetPrices([FromUri]Article articles) {
或者
[HttpPost]
public void GetPrices(Article articles)
你把你要返回的东西放在哪里而不是 void ?
.controller('LoginController', ['$scope', '$http', function ($scope, $http) {
function getPrices(articles) {
$http.get('http://someurl/api/prices/getprices')
.success(function (data) {
articles: data
}
}
}])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.