Below is the complete JS code snippet:
function getPoolsData(){
$.getJSON('../json/data.json', function(data) {
var date_from = new Date();
console.log(date_from);
var pools_hashrates = [{"date_from" : date_from}];
data.pools.forEach(function(pool){
var api_url = pool.api;
var poolName = pool.name;
if(pool.type == "forknote"){
$.getJSON(api_url + 'stats', function(data) {
var poolHashrate = data.pool.hashrate;
pools_hashrates.push({"poolName" : poolName, "hashrate" : poolHashrate});
console.log("Pool name: " + poolName + " Pool hashrate: " + parseInt(poolHashrate));
});
}
else{
$.getJSON(api_url + 'pool/stats', function(data) {
var poolHashrate = data.pool_statistics.hashRate;
console.log("Pool name: " + poolName + " Pool hashrate: " + parseInt(poolHashrate));
pools_hashrates.push({"poolName" : poolName, "hashrate" : poolHashrate});
});
}
});
console.log(pools_hashrates);
$.ajax({
type: "POST",
contentType : 'application/json; charset=utf-8',
dataType : 'json',
url: "/save",
data: JSON.stringify(pools_hashrates),
success :function(result) {
console.log("Success!");
}
});
});
}
Here's how the controller method looks like:
@RequestMapping("/save")
public @ResponseBody String getPoolsData(@RequestBody String string){
System.out.println("Triggered: " + string);
return "Success mvc";
}
And this is the output from the controller:
Triggered: [{"date_from":"2018-04-13T11:05:00.652Z"}]
The issue at hand is that only the first index of the array is being sent to the controller, even though the array contains around 20 items. The entire array is displayed when console.log(pools_hashrates)
is called. The script is triggered by a button.