0

My angularJs function returns response in Json, but I am not been able to get the 'model' part out from it.

Below is my code:

        this.search = function () {
    var response = $http({
        method: 'GET',
        url: '/api/TalentPool/Search'
    });

    return response;
}

this.search().then(function (response) {
    console.log('conscole: ' + response.data.model)
})

this.search().then(function (response) {
    console.log(response.data.model)
})

And below is my mvc method:`

  List<CandidateSearchViewModel> output = CRBuilderObj.ContructResultsViewModel(data);
            CandidateSearch.model = output;
            CandidateSearch.baseCriteria = criteria;
            return Ok(CandidateSearch);
User
  • 79
  • 1
  • 11
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Heretic Monkey Jun 07 '17 at 14:47
  • where do you catch the promise – Sachila Ranawaka Jun 07 '17 at 14:48
  • You can't return `response` like that. [Read the documentation](https://docs.angularjs.org/api/ng/service/$http). – Heretic Monkey Jun 07 '17 at 14:49
  • You might be correct, but I have seen that post and not been able to find the solution. – User Jun 07 '17 at 14:49
  • where you resolve promise ? where is `.then()`, See this [example fiddle](http://jsfiddle.net/xmwdhm1r/) – anoop Jun 07 '17 at 14:54
  • My understanding is that its optional to have "promise" – User Jun 07 '17 at 14:56
  • `$http.get()` itself return the `promise`, you just need to resolve it by `.then()`, and by default, it handles JSON parsing. – anoop Jun 07 '17 at 14:57

1 Answers1

0

if you want to access the response of the http request then you have to resolve the promise firs. then access the model property from the response

this.search = function () {
    return $http({
        method: 'GET',
        url: '/api/TalentPool/Search'
    });
}

//resolve the promise like this 

this.search().then(function(response){
     console.log(response.data.model)
})
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80