简体   繁体   English

如何获取角度js中的数据,这些数据是作为json响应从节点js发送的(表示js middle ware)

[英]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. 我想要做的是在用户登录后,将令牌以json格式发送到角度模块或其他格式无关紧要,然后我想将其保存在cookie中。

Here is a simple json response code from node js 这是来自节点js的简单json响应代码

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. 请指导我现在如何在角度控制器中获取它,然后如何在角度cookie中保存它。

Thanks. 谢谢。

This is the edited part guys 这是编辑过的部分人

This is my controller's code which calls node js route 这是我的控制器代码,它调用节点js路由

    $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 这是整个过程完成后由nodejs做出的响应

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

Now this is the cookie part which I followed from the answers given below. 现在这是我从下面给出的答案中遵循的cookie部分。

    $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 编辑没有2个人

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. 我能够得到我想要的但错误现在与cookie有关。

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. 我上面提到的cookie问题已经解决了,原因很简单,因为我忘记了使用商店一词。

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. 谢谢你的时间和帮助。

Call the nodejs API which sent this token in angularjs 调用在angularjs中发送此令牌的nodejs API

To call the nodejs API from angularjs use angular $http module and if your node API are RestAPI then use $resource for better control over API calls. 要从angularjs调用nodejs API,请使用angular $http模块,如果您的节点API是RestAPI,则使用$resource更好地控制API调用。

Example : 示例:

$http.get('apicall');

Assumning you have a controller set up, you would simply have to make a call with the $http call. 假设您已设置控制器,您只需使用$http呼叫进行呼叫即可。

$http.get(URL) would make the call via GET method, $http.post(url) would make it a POST. $http.get(URL)将通过GET方法进行调用, $http.post(url)将使其成为POST。

After that, to get the data you need to use promises that $http returns.I've never used the $cookies service but it seems straightforward enough: 之后,要获取数据,您需要使用$ http返回的承诺。我从未使用过$cookies服务,但它看起来很简单:

$http.get(URL).success(function(data){
    $cookie.put('token',data.token)
}).error(function(error){
    console.log(error)
})

I would strongly recommend reading the AngularJS tutorial to learn a little more about the $http service, promises and how to handle them. 我强烈建议您阅读AngularJS教程,以了解有关$http服务,promises以及如何处理它们的更多信息。

Consider you have a API and it returns a json serialized object. 考虑你有一个API,它返回一个json序列化对象。

In Angular Controller: 在角度控制器中:

 $http.get("ur Api Url or path").then(function(data){
   //data will hold json result
  // First parse the "data" variable like data=JSON.parse(data);
  // Then you can save it to cookie or use angularjs  Value
  ....
  }).error(function(error){
 ...
  });

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. 这是角度JS控制器的代码。

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM