AngularJS $http response [duplicate]












2
















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




here is my code



var object = null
app.controller("controller",function($scope,service){
$scope.search=function(){
service.findData().then(
function successCallback(response){
object = response.data
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
})


when I print the object, it is null so the question is: how can I get response.data outside?



Thank you for helping!










share|improve this question















marked as duplicate by georgeawg angularjs
Users with the  angularjs badge can single-handedly close angularjs questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 14:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    write console.log inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with $scope.object = ... and then display it with {{object}} for example

    – Aleksey Solovey
    Nov 22 '18 at 13:22






  • 1





    yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()

    – J.Smith
    Nov 22 '18 at 13:28






  • 1





    then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with .then and still keep the logic inside the Promise

    – Aleksey Solovey
    Nov 22 '18 at 13:34











  • @AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.

    – Jins Peter
    Nov 22 '18 at 13:45
















2
















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




here is my code



var object = null
app.controller("controller",function($scope,service){
$scope.search=function(){
service.findData().then(
function successCallback(response){
object = response.data
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
})


when I print the object, it is null so the question is: how can I get response.data outside?



Thank you for helping!










share|improve this question















marked as duplicate by georgeawg angularjs
Users with the  angularjs badge can single-handedly close angularjs questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 14:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    write console.log inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with $scope.object = ... and then display it with {{object}} for example

    – Aleksey Solovey
    Nov 22 '18 at 13:22






  • 1





    yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()

    – J.Smith
    Nov 22 '18 at 13:28






  • 1





    then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with .then and still keep the logic inside the Promise

    – Aleksey Solovey
    Nov 22 '18 at 13:34











  • @AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.

    – Jins Peter
    Nov 22 '18 at 13:45














2












2








2









This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




here is my code



var object = null
app.controller("controller",function($scope,service){
$scope.search=function(){
service.findData().then(
function successCallback(response){
object = response.data
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
})


when I print the object, it is null so the question is: how can I get response.data outside?



Thank you for helping!










share|improve this question

















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




here is my code



var object = null
app.controller("controller",function($scope,service){
$scope.search=function(){
service.findData().then(
function successCallback(response){
object = response.data
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
})


when I print the object, it is null so the question is: how can I get response.data outside?



Thank you for helping!





This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers








angularjs angularjs-scope angular-promise






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 15:13









the_ultimate_developer

1,006620




1,006620










asked Nov 22 '18 at 13:20









J.SmithJ.Smith

132




132




marked as duplicate by georgeawg angularjs
Users with the  angularjs badge can single-handedly close angularjs questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 14:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by georgeawg angularjs
Users with the  angularjs badge can single-handedly close angularjs questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 14:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    write console.log inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with $scope.object = ... and then display it with {{object}} for example

    – Aleksey Solovey
    Nov 22 '18 at 13:22






  • 1





    yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()

    – J.Smith
    Nov 22 '18 at 13:28






  • 1





    then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with .then and still keep the logic inside the Promise

    – Aleksey Solovey
    Nov 22 '18 at 13:34











  • @AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.

    – Jins Peter
    Nov 22 '18 at 13:45














  • 2





    write console.log inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with $scope.object = ... and then display it with {{object}} for example

    – Aleksey Solovey
    Nov 22 '18 at 13:22






  • 1





    yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()

    – J.Smith
    Nov 22 '18 at 13:28






  • 1





    then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with .then and still keep the logic inside the Promise

    – Aleksey Solovey
    Nov 22 '18 at 13:34











  • @AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.

    – Jins Peter
    Nov 22 '18 at 13:45








2




2





write console.log inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with $scope.object = ... and then display it with {{object}} for example

– Aleksey Solovey
Nov 22 '18 at 13:22





write console.log inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with $scope.object = ... and then display it with {{object}} for example

– Aleksey Solovey
Nov 22 '18 at 13:22




1




1





yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()

– J.Smith
Nov 22 '18 at 13:28





yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()

– J.Smith
Nov 22 '18 at 13:28




1




1





then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with .then and still keep the logic inside the Promise

– Aleksey Solovey
Nov 22 '18 at 13:34





then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with .then and still keep the logic inside the Promise

– Aleksey Solovey
Nov 22 '18 at 13:34













@AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.

– Jins Peter
Nov 22 '18 at 13:45





@AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.

– Jins Peter
Nov 22 '18 at 13:45












1 Answer
1






active

oldest

votes


















2














It was a general practice during the era of AngularJS to assign the controller to variable.



app.controller("controller",function($scope,service){
var self = this;
$scope.search=function(){
service.findData().then(
function successCallback(response){
self.yourBusinessFunction (response.data)

},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}

self.yourBusinessFunction = function(data){

console.log(data);
// data here will be the response.data from the service
// You can write your logic here

}


})





share|improve this answer


























  • tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data

    – J.Smith
    Nov 22 '18 at 14:06













  • sync both responses with $q.all([promise1, promise2]).then(function(response_array){...})

    – Aleksey Solovey
    Nov 22 '18 at 14:25













  • thx,i think i can solve it today :)

    – J.Smith
    Nov 23 '18 at 3:26











  • It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property of self like self.businessFunction2 in th same controller and use it likewise.

    – Jins Peter
    Nov 23 '18 at 5:12











  • i get it,this seems to be easier;

    – J.Smith
    Nov 23 '18 at 7:54


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














It was a general practice during the era of AngularJS to assign the controller to variable.



app.controller("controller",function($scope,service){
var self = this;
$scope.search=function(){
service.findData().then(
function successCallback(response){
self.yourBusinessFunction (response.data)

},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}

self.yourBusinessFunction = function(data){

console.log(data);
// data here will be the response.data from the service
// You can write your logic here

}


})





share|improve this answer


























  • tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data

    – J.Smith
    Nov 22 '18 at 14:06













  • sync both responses with $q.all([promise1, promise2]).then(function(response_array){...})

    – Aleksey Solovey
    Nov 22 '18 at 14:25













  • thx,i think i can solve it today :)

    – J.Smith
    Nov 23 '18 at 3:26











  • It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property of self like self.businessFunction2 in th same controller and use it likewise.

    – Jins Peter
    Nov 23 '18 at 5:12











  • i get it,this seems to be easier;

    – J.Smith
    Nov 23 '18 at 7:54
















2














It was a general practice during the era of AngularJS to assign the controller to variable.



app.controller("controller",function($scope,service){
var self = this;
$scope.search=function(){
service.findData().then(
function successCallback(response){
self.yourBusinessFunction (response.data)

},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}

self.yourBusinessFunction = function(data){

console.log(data);
// data here will be the response.data from the service
// You can write your logic here

}


})





share|improve this answer


























  • tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data

    – J.Smith
    Nov 22 '18 at 14:06













  • sync both responses with $q.all([promise1, promise2]).then(function(response_array){...})

    – Aleksey Solovey
    Nov 22 '18 at 14:25













  • thx,i think i can solve it today :)

    – J.Smith
    Nov 23 '18 at 3:26











  • It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property of self like self.businessFunction2 in th same controller and use it likewise.

    – Jins Peter
    Nov 23 '18 at 5:12











  • i get it,this seems to be easier;

    – J.Smith
    Nov 23 '18 at 7:54














2












2








2







It was a general practice during the era of AngularJS to assign the controller to variable.



app.controller("controller",function($scope,service){
var self = this;
$scope.search=function(){
service.findData().then(
function successCallback(response){
self.yourBusinessFunction (response.data)

},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}

self.yourBusinessFunction = function(data){

console.log(data);
// data here will be the response.data from the service
// You can write your logic here

}


})





share|improve this answer















It was a general practice during the era of AngularJS to assign the controller to variable.



app.controller("controller",function($scope,service){
var self = this;
$scope.search=function(){
service.findData().then(
function successCallback(response){
self.yourBusinessFunction (response.data)

},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}

self.yourBusinessFunction = function(data){

console.log(data);
// data here will be the response.data from the service
// You can write your logic here

}


})






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 6:29

























answered Nov 22 '18 at 13:42









Jins PeterJins Peter

1,283520




1,283520













  • tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data

    – J.Smith
    Nov 22 '18 at 14:06













  • sync both responses with $q.all([promise1, promise2]).then(function(response_array){...})

    – Aleksey Solovey
    Nov 22 '18 at 14:25













  • thx,i think i can solve it today :)

    – J.Smith
    Nov 23 '18 at 3:26











  • It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property of self like self.businessFunction2 in th same controller and use it likewise.

    – Jins Peter
    Nov 23 '18 at 5:12











  • i get it,this seems to be easier;

    – J.Smith
    Nov 23 '18 at 7:54



















  • tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data

    – J.Smith
    Nov 22 '18 at 14:06













  • sync both responses with $q.all([promise1, promise2]).then(function(response_array){...})

    – Aleksey Solovey
    Nov 22 '18 at 14:25













  • thx,i think i can solve it today :)

    – J.Smith
    Nov 23 '18 at 3:26











  • It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property of self like self.businessFunction2 in th same controller and use it likewise.

    – Jins Peter
    Nov 23 '18 at 5:12











  • i get it,this seems to be easier;

    – J.Smith
    Nov 23 '18 at 7:54

















tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data

– J.Smith
Nov 22 '18 at 14:06







tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data

– J.Smith
Nov 22 '18 at 14:06















sync both responses with $q.all([promise1, promise2]).then(function(response_array){...})

– Aleksey Solovey
Nov 22 '18 at 14:25







sync both responses with $q.all([promise1, promise2]).then(function(response_array){...})

– Aleksey Solovey
Nov 22 '18 at 14:25















thx,i think i can solve it today :)

– J.Smith
Nov 23 '18 at 3:26





thx,i think i can solve it today :)

– J.Smith
Nov 23 '18 at 3:26













It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property of self like self.businessFunction2 in th same controller and use it likewise.

– Jins Peter
Nov 23 '18 at 5:12





It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property of self like self.businessFunction2 in th same controller and use it likewise.

– Jins Peter
Nov 23 '18 at 5:12













i get it,this seems to be easier;

– J.Smith
Nov 23 '18 at 7:54





i get it,this seems to be easier;

– J.Smith
Nov 23 '18 at 7:54





Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory