【问题标题】:Ajax returns only first index of stringified array to the Spring controllerAjax 仅将字符串化数组的第一个索引返回给 Spring 控制器
【发布时间】:2018-09-23 19:42:10
【问题描述】:

这是整个 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!");
 }
});

});
}

这里是控制器方法:

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

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

还有一个控制器输出:

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

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

【问题讨论】:

    标签: javascript json ajax spring-mvc stringify


    【解决方案1】:

    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
    

    【讨论】:

    • 他们三个?只有1个ajax调用?抱歉,我是 Java 开发人员,所以这对我来说很新。
    • $.getJson 只是 ajax get 调用的简写,实际上每个 for 循环都有 ajax 调用,请参见此处api.jquery.com/jquery.getjson
    • 哇。那么,我需要在每个特定的 ajax 调用上设置一个间隔和.active 标志吗?
    • jQuery.active 只是为您提供了您拥有的活动 ajax 调用的数量,因此您只需要对最后一个调用执行此操作,因为它是唯一使用另一个数据的调用
    • 它有效,但我必须从setInterval 之后开始删除一对括号。谢谢楼主:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2021-08-02
    • 2015-12-22
    • 2017-02-22
    • 2018-06-16
    • 1970-01-01
    • 2011-12-02
    相关资源
    最近更新 更多