0

I am trying to return the output of my service below to controller's calling variable "response". The service does gets called and it also have an output however it never gets back to my calling again. What Am i doing wrong here?

this.AssociatetoJob1 = function (application) {
    var config = {
        headers: {
            'contentType': 'application/json; charset=utf-8;'
        }
    };
    return $http.post('/api/Application/AddApplication', 
        JSON.stringify(application), config).then(function (result) {
            return result.data;
        });     
}

And below is the line from my calling Controller:

var response = CandidateService.AssociatetoJob1(appInfom);
Nikolaj Dam Larsen
  • 5,455
  • 4
  • 32
  • 45
User
  • 79
  • 1
  • 11
  • Probably a duplicate of [this](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) but I'm really unsure what "it never gets back to my calling again" means or how you are determining that happens (since there's no sign of the `response` variable ever being inspected). – Quentin Jul 27 '17 at 09:39
  • sorry not clear what you mean? – User Jul 27 '17 at 09:40
  • That makes two of us. – Quentin Jul 27 '17 at 09:42
  • you should do like blow : CandidateService.AssociatetoJob1(appInfom).then(function(response){ response = response.data }) – user1608841 Jul 27 '17 at 09:42
  • Could you try to add an error callback handler and log it? maybe you are getting a 404 or something other http error that you are not catching. – Hannoun Yassir Jul 27 '17 at 09:42

3 Answers3

0

Your contentType is wrong.Change it like this Content-Type

JS:

    this.AssociatetoJob1 = function (application) {
    var config = {
        headers: {
            'Content-Type': 'application/json; charset=utf-8;'
        }
    }
    return $http.post('/api/Application/AddApplication', JSON.stringify(application), config).then(function (result) {
        return result.data;
    });

}

HTML

var response = CandidateService.AssociatetoJob1(appInfom);

You can get the details returned by your API call by using .then on the promise which was resolved.

response.then(function(result){
  console.log(result.data);
})
Vivz
  • 6,625
  • 2
  • 17
  • 33
0
this.AssociatetoJob1 = function (application) {
    var config = {
        headers: {
            'contentType': 'application/json; charset=utf-8;'
        }
    }
    return $http.post('/api/Application/AddApplication', JSON.stringify(application), config).then(function (result) {
        return result.data;
    });

}

above code does return you Promise object. So as to utilize this you need to do something like below in controller:

  var responseData ; 
  CandidateService.AssociatetoJob1(appInfom).then(function(res‌​ponse){ 
     responseData  = response.data ;
    })
user1608841
  • 2,455
  • 1
  • 27
  • 40
0

In your "this.AssociatetoJob1" function, you called the service like

return $http.post('/api/Application/AddApplication', JSON.stringify(application), config).then(function (result) {
    return result.data;
});

In this you have used a callback function while calling the service, Instead of this, call the service like

return $http.post('/api/Application/AddApplication', JSON.stringify(application), config);

that means just remove the .then() callback from your service call and call "this.AssociatetoJob1" method like

this.AssociatetoJob1(application).then(function(response){console.log(response.Data)});

Hope It helped you...

Vaijinath Harbak
  • 149
  • 3
  • 10