0

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.

12
  • how it not working? errors in browser console? errors on server? do nothing? Commented Oct 2, 2015 at 19:28
  • first: IEnumerable<FrequentPawnerReport> Get if you return collection - why here 'get' : {method: 'GET', isArray: false }, you set flag isArray to false? Commented Oct 2, 2015 at 19:30
  • Its not calling the WebApi routine at all. My understanding is that if you set isArray to false, it can return an object. Commented Oct 2, 2015 at 19:55
  • Can you provide how you setup routing? Commented Oct 3, 2015 at 12:04
  • This is the Web Api webapiconfig.cs Register method: Commented Oct 3, 2015 at 13:13

1 Answer 1

0

i see a few errors in your code.

  1. your get method return collection, so here 'get' : {method: 'GET', isArray: false }, isArray flag should be true, otherwise you get error from angular, when get response.

  2. your Get method without parameters, so possibly you need something like

    public IEnumerable<string> Get(FrequentPawnerReportCriteria criteria)
    
  3. you confuse Get and Post methods. So, possibly, this action FrequentPawnerReport should be a post, and return just one object.

    public FrequentPawnerReport Post([FromBody] FrequentPawnerReportCriteria criteria)
    {
        var repo = new FrequentPawnerReport();
        return repo.GetReport(criteria);
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

Why a Post method if I am retrieving data? The criteria object I am passing is a collection of the criteria to send through the Controller to the Repository method. Also, its not hitting Web Api controller at all. I even changed the call to retrieve the parameter-less Get and it didn't call that.
@rsalit, so you use it wrong :-) you try call from js action without params, because for action with params you add another route

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.