I'm trying to call a WebApi method from an angularjs $http function. I've tried using a standard get, query and an action name but I was corrected realizing that you can't pass an object with Get. So I'm using Post. The Web Api is being called and is returning the value I expect. However, the angularjs side is not getting the value. Here is the latest, that isn't working:
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);
}
}
criteria object:
vm.criteria = { maxResults: 25, startDate: new Date(2014, 10, 1), endDate: new Date(2014, 11, 1), isActive: true, transTypeId: 1, jurisdictions: [], reportType: 1, relationship: 1, make: '', propertyGroupId: 0, propertyTypeId: 0, jurisdictionCount: 0, storeCount: 0, useTypePawn: false, useTypeScrap: false }
Any ideas are appreciated.
IEnumerable<FrequentPawnerReport> Getif you return collection - why here'get' : {method: 'GET', isArray: false },you set flag isArray to false?