我想把获取的json里面的数据一个个存到obj数组里,但是一但出了$.getJson()方法,obj的长度就为0了

解决方案 »

  1.   

    异步了,each加个计数器
      

  2.   

    如上所述,异步了,加个then就好            var obj = [];
                $.getJSON(url, (data, status) => {
                    $.each(data, (index, value) => {
                        obj.push(value);
                    });
                    console.log("内部:"+obj.length)
                }).then(res => {
                    console.log("外部2:"+obj.length)
                });
                console.log("外部1:"+obj.length)
      

  3.   

    如果你不想整其他的话  就在$.getJSON()方法前加 一行代码$.ajaxSettings.async = false;