類似這樣:var aaa = 0;http.get('http://abc.com/api"', function (res) {var json = '';res.on('data', function (d) { json += d;});res.on('end',function(){ //獲取到的數(shù)據(jù) json = JSON.parse(json); aaa = json;});}).on('error', function (e) {console.error(e);});console.log(aaa);因為異步返回數(shù)據(jù)的問題,實際上aaa沒有得到j(luò)son的數(shù)據(jù),輸出還是0;如何讓aaa的輸出為json的值呢?
node.js中json獲取到的變量如何轉(zhuǎn)化為全局變量?
九州編程
2019-03-13 14:19:07