Gandalf the White Gandalf the White - 1 year ago 56
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.


This is the edited part guys

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

$'../userlogin', $scope.user).success(function(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.

$'../userlogin', $scope.user).success(function(response) {


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;
$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.


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 Source

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.

      var z = response.token; 
      $cookieStore.put('token', z);
    var f = $cookieStore.get('token');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download