【发布时间】:2015-12-31 02:10:17
【问题描述】:
我正在尝试从 angularjs $http 函数调用 WebApi 方法。我尝试使用标准的 get、查询和操作名称,但我更正地意识到您不能使用 Get 传递对象。所以我正在使用邮政。正在调用 Web Api 并返回我期望的值。但是,angularjs 方面没有获得价值。这是最新的,但不起作用:
WebAPI
[RoutePrefix("api/frequentpawner")]
public class FrequentPawnerController : ApiController
{
[HttpPost]
public HttpResponseMessage Post([FromBody] FrequentPawnerReportCriteria criteria)
{
var repo = new FrequentPawnerReport();
var result = repo.GetReport(criteria);
var httpResult = new HttpResponseMessage(HttpStatusCode.OK);
var jsonMediaTypeFormatter = new JsonMediaTypeFormatter
{
SerializerSettings =
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}
};
httpResult.Content = new ObjectContent<List<FrequentPawnerReport>>(result, jsonMediaTypeFormatter);
return httpResult;
}
function getFrequentPawner(criteria) {
return $http.post("/api/FrequentPawner/Post", criteria)
.then (getFrequentPawnerComplete)
.catch(getFrequentPawnerFailed);
function getFrequentPawnerComplete(response) {
var x = response.data;
return response.data.results;
}
function getFrequentPawnerFailed(error) {
alert("XHR failed for frequent pawner report: " + error.responseText);
}
}
条件对象:
vm.criteria = { 最大结果:25, 开始日期:新日期(2014 年 10 月 1 日), endDate: 新日期(2014, 11, 1), isActive:真, transTypeId: 1, 司法管辖区:[], 报告类型:1, 关系:1, 制作: '', 属性组ID:0, 属性类型ID:0, 管辖权数:0, 商店计数:0, 使用TypePawn:假, 使用TypeScrap:假 }
感谢任何想法。
【问题讨论】:
-
它如何不工作?浏览器控制台中的错误?服务器上的错误?什么都不做?
-
first:
IEnumerable<FrequentPawnerReport> Get如果你返回集合 - 为什么在这里'get' : {method: 'GET', isArray: false },你将标志 isArray 设置为 false? -
它根本没有调用 WebApi 例程。我的理解是,如果将isArray设置为false,它可以返回一个对象。
-
你能提供你如何设置路由吗?
-
这是Web Api webapiconfig.cs 注册方法:
标签: c# angularjs asp.net-web-api