dra87370 2016-05-19 17:42 采纳率: 100%
浏览 55
已采纳

Jquery POST没有填充数组

I am trying to populate an array with a JSON entries in reference to HighCharts.

One of the JSON objects is manually populated and one needs to be populated dynamically with a PHP post. I have been trying to use a callback function to push data to the array, but no data seems to get pushed.

I am able to verify the data comes back from the PHP Post. It seems like it does populate the array, but once the Jquery POST function is complete, it seems like the array is emptied.

Also, I have a console.table call inside of the POST. This one actually prints the array correctly.

You can see from the code below, that I use a console.table() call immediately after the post, and it is empty. But after I push manually next, the data array now has the data inside of it for the manually JSON array push.

Any ideas?

Here is my code:

/* INITIAL ARRAY **************************************/
data_array = [];
var colors = Highcharts.getOptions().colors;
categories = ['Dynamic', 'Manual'];

/* FUNCTION TO PUSH TO ARRAY *****************************/
function push_toarray(cats,counts) {

    data_array.push({
        y: 10.38,
        color: colors[1],
        drilldown: {
            name: 'Dynamically Populated',
            categories: [cats],
            data: [counts],
            color: colors[1]
        }
    });       
}

/* PHP AND FOR LOOP FOR GETTING DATA *********************************************/


    $.post( "php/dt_charts.php")
        .done(function( data ) {
        var obj = jQuery.parseJSON(data);

        // To verify the values are returned
        console.log(obj.fe_det_name);  
        console.log(obj.fe_det_count);

        push_toarray(obj.fe_det_name, obj.fe_det_count);
        console.table(data_array);  // This prints the array correctly with both JSON objects inside
    });

    //console.table(data_array);  // This prints no data.

/* MANUAL DATA ARRAY *********************************************/


    data_array.push({
        y: 10.38,
        color: colors[1],
        drilldown: {
            name: 'Manually Populated',
            categories: ['Firefox v31', 'Firefox v32', 'Firefox v33', 'Firefox v35', 'Firefox v36', 'Firefox v37', 'Firefox v38'],
            data: [0.33, 0.15, 0.22, 1.27, 2.76, 2.32, 2.31, 1.02],
            color: colors[1]
        }
    })


console.table(data_array); // This prints only the manually populated array values
  • 写回答

1条回答 默认 最新

  • dpr81047 2016-05-19 18:15
    关注
      $.post( "php/dt_charts.php")
         .done(function( data ) {
        var obj = jQuery.parseJSON(data);
    
        // To verify the values are returned
        console.log(obj.fe_det_name);  
        console.log(obj.fe_det_count);
    
        push_toarray(obj.fe_det_name, obj.fe_det_count);
        console.table(data_array);  // This prints the array correctly with both JSON objects inside
    
    <----- move your logic here,
    
    });
    

    Move your logic there and if you cant than put your logic in a function and call that function inside your done callback.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。