Gandalf the White Gandalf the White - 5 months ago 10
Node.js Question

how to fetch data in angular js that is sent as json response from node js (express js middle ware)

So I'm working on tokens that I am making manually. What I want to do is after user logins a token is sent to angular module in json format or some other format doesn't matter and then I'd like to save it in a cookie.

Here is a simple json response code from node js

res.json({success : 1, token: "adhahashahd"});


please guide me now how to fetch this in angular controller and then how to save this in angular cookie.

Thanks.


This is the edited part guys


This is my controller's code which calls node js route

$http.post('../userlogin', $scope.user).success(function(response) {
console.log(response);


This is the response which is made by nodejs after the whole process is complete

res.json({token : "asx"});


Now this is the cookie part which I followed from the answers given below.

$http.post('../userlogin', $scope.user).success(function(response) {
console.log(response);




$http.get('userlogin').success(function(data){
data=JSON.Parse(data);
$cookie.put('token',data.token)
}).error(function(error){
console.log(error)
})


The error comes on the front end part, as seen in developer window of google chrome

GET http://localhost:1339/userlogin 404 (Not Found)



Up voted all of you, thanks guys. One question if all answers are good enough can I accept multiple answers. Because all you guys gave correct suggestion.


I am really thankful. :)


Edit no 2 guys


When I am simply doing this

var z = response.token;
console.log(z);
$cookie.put('token', z);


I am able to get what I wanted but the error is related to cookie now.

It reads

ReferenceError: $cookie is not defined
at controller.js:35
at angular.js:9369
at angular.js:13189
at l.c.$get.l.$eval (angular.js:14401)
at l.c.$get.l.$digest (angular.js:14217)
at l.c.$get.l.$apply (angular.js:14506)
at l (angular.js:9659)
at P (angular.js:9849)
at XMLHttpRequest.w.onload (angular.js:9790)


fetching the data is done, although not by the method you guys suggested. What I am doing is right or not conventionally? Please let me know with reasons that will help me to understand the topic in a better manner.

Thanks


Last Edit


The cookie problem I mentioned above is solved, it was simply because I had forgotten to use the store word along with it.

The code you guys gave me, I have nothing against it but things just worked simply for me. It may be wrong conventionally because of things which I don't understand probably.

That is why I request you guys to further add anything if necessary.

Thanks for your time and help.

Answer

First of all, I would like to thank all the people who have helped me along the way.

And without any disrespect to people who have answered, I am answering this question with different answer that worked.

Here is the format in which the data is being sent from the server as response, which is also given in the question posted above.

res.json({token : "asx"});

This is the code for angular JS controller.

Ignore various console.log because it was done just to make sure that the code is working properly according to my needs and it is working like a charm.

   console.log(response.token);
      var z = response.token; 
      console.log(z);
      $cookieStore.put('token', z);
    var f = $cookieStore.get('token');
    console.log(f);
Comments