简体   繁体   中英

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

I am getting json data successfully from a web API. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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