javascript - Ajax 仅将字符串化数组的第一个索引返回给 Spring Controller

标签 javascript json ajax spring-mvc stringify

这是完整的 JS 代码:

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!");
 }
});

});
}

这是 Controller 方法:

@RequestMapping("/save")
public @ResponseBody String getPoolsData(@RequestBody String string){

    System.out.println("Triggered: " + string);
    return "Success mvc";
}

Controller 输出:

Triggered: [{"date_from":"2018-04-13T11:05:00.652Z"}]

问题是,只有数组的第一个索引被发送到 Controller ,而数组的长度约为 20。 console.log(pools_hashrates) 打印整个数组。该脚本通过按钮调用。

最佳答案

Ajax 调用是异步的,这意味着它将同时触发所有 3 个调用,对 getPoolsData 的调用不会等待 get 完成,您需要将 ajax 调用设置为异步。

像这样

$.ajaxSetup({
    async: false
});

请注意,这会将所有 ajax 调用设置为异步,更好的做法是重写您的调用

$.ajax({
    url: "...",
    type: "GET",
    data: ...,
    async: false
});

仅使这些调用异步

或者你可以使用 setInterval 来继续检查 jQuery.active == 0

jQuery.active == 0 // this tells you if you have active ajax calls

如果是这样

var myTimer = setInterval((function(){ 
    if (jQuery.active == 0){
        $.ajax({
            type: "POST",
            contentType : 'application/json; charset=utf-8',
            dataType : 'json',
            url: "/save",
            data: JSON.stringify(pools_hashrates),
            success :function(result) {
                console.log("Success!");
            }
        });
        clearInterval(myTimer); // stop the interval once you the get calls finished and you send the ajax call
    }
}, 1000)); // 1000 is the interval at which to check set in miliseconds

关于javascript - Ajax 仅将字符串化数组的第一个索引返回给 Spring Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49817122/

相关文章:

子菜单的javascript悬停功能

javascript - 使用 Canvas three.js 中的数据创建纹理

javascript - 发送前更改复选框默认值

c# - 使用 NEST 的 ElasticSearch 索引/插入失败

javascript - ajax 调用后将 bool 值重置为 false

javascript - Ajax 调用 Razor 中的 Web 服务

javascript - Jasmine - 监视构造函数中的方法调用

c# - JSON 响应不包含所有项目

c# - 如何从 JSON 对象解析整数列表

jQuery UI 选项卡 AJAX 链接与 anchor