简体   繁体   English

如何将此json数据从服务器传递到控制器? Node.js / AngularJS

[英]How to pass this json data from server to controller? Nodejs / AngularJS

I am getting json data successfully from a web API. 我正在从Web API成功获取json数据。 It is logging correctly to server console, but when I pas it to client it is coming as blank [] . 它已正确记录到服务器控制台,但是当我将其粘贴到客户端时,它显示为空白[]

What am I doing wrong? 我究竟做错了什么?

 app.get('/balance', function(req, res) { var poloniexExchange = new Cryptox( "poloniex", { key: 'IGVRQCXB', secret: '93b5686d9ef' }); //get all account balances poloniexExchange.getBalance({ account: 'all' }, function(err, balance) { if (err) console.log(err); if (!err) console.log(balance.data[0].total); //logging all json data res.json(balance.data[0].total); }); }); 

 app.controller('websocketController', function($scope, $http) { $http.get("/balance") .then(function(response) { console.log(response.data); //logging '[]' }); }); 

I need to pass balance.data[0].total from server to controller. 我需要从服务器到控制器传递balance.data[0].total What am I doing wrong and How can I make this work? 我在做什么错了,我该怎么做?

Here's a question: Are you running your dev server on the same machine you're making the query from? 这是一个问题:您是否在进行查询的同一台计算机上运行开发服务器? If so: your problem is likely that you'd need to include the following header series on your server. 如果是这样:您的问题很可能需要在服务器上包括以下标头系列。

res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

If I recall correctly, the basic reasoning is that most web clients don't like accepting responses from local HTTP requests if they're not all from the same domain, so they need another layer of confirmation that this isn't an attack. 如果我没记错的话,基本原因是大多数Web客户端不喜欢接受来自本地HTTP请求的响应(如果它们不是来自同一域的话),因此它们需要另一层确认这不是攻击。 Look into cross domain attacks to find out more. 调查跨域攻击以了解更多信息。

keep in mind, the * means that it'll go with anything, so you'll need to either get specific whith the values to accept there, or remove it and avoid running queries from the server's host before you try to ship the production code. 请记住,*表示它将与任何内容一起使用,因此在尝试交付生产代码之前,您需要获取特定的值以接受该值,或者将其删除并避免从服务器主机运行查询。

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

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