JSON数据解析不起作用

时间:2021-07-03 15:34:26

guys please see Javascript code below:

伙计们请看下面的Javascript代码:

$(document).ready(function () {
$('.loader').hide();
getDataUsers("userCount");
var obj = JSON.parse($("#hdUserCount").val());
userChart(obj.Actives, obj.InActives, obj.Discountinued, obj.recentlyJoined);
});
function userChart(act, inact, disc, recent) {
var chart = new CanvasJS.Chart("userChart", {

                                   title: {
        text: "Users"

    },
                                   animationEnabled: true,
                                   axisX: {
        interval: 1,
        gridThickness: 0,
        labelFontSize: 10,
        labelFontStyle: "normal",
        labelFontWeight: "normal",
        labelFontFamily: "Lucida Sans Unicode"

    },
                                   axisY2: {
        interlacedColor: "rgba(1,77,101,.2)",
        gridColor: "rgba(1,77,101,.1)"

    },

                                   data: [{
                                               type: "bar",
                                               name: "Users",
                                               axisYType: "secondary",
                                               color: "#014D65",
                                               dataPoints: [

                                                   { y: act, label: "Active" },
                                                   { y: inact, label: "InActive" },
                                                   { y: disc, label: "DisContinued" },
                                                   { y: recent, label: "Recently Joined" },
                                               ]
                                           }

    ]
                               });

chart.render();
}
function getDataUsers(flagData) {
$('.loader').fadeIn('fast');
$.post("Stats.ashx", {
           flag: flagData,
       }, function (data, status) {
           $("#hdUserCount").val(data);
           $('.loader').fadeOut('fast');
           alert(data);
       });
}

and the problem is that when it runs in debug mode in FireBug and line 3 to 5 has break point there is no problem, and every thing is good. BUT! when I remove break points I think some parts of the code are not being executed!!! wired!!! I think the part that parses the JSON data is not working!!!

问题是当它在FireBug中以调试模式运行时,第3行到第5行有断点时没有问题,而且每件事都很好。但!当我删除断点时,我认为代码的某些部分没有被执行!有线!!!我认为解析JSON数据的部分不起作用!

and this is the data received by post:

这是邮寄的数据:

{"Actives": 3, "InActives": 4, "Discountinued": 0, "recentlyJoined": 6}

{“Actives”:3,“InActives”:4,“Discountinued”:0,“recentJoined”:6}

1 个解决方案

#1


1  

That's not how asynchronous code works. This is what your function should look like:

这不是异步代码的工作原理。这是你的功能应该是这样的:

function getDataUsers(flagData, callback) {
    $('.loader').fadeIn('fast');
    $.post("Stats.ashx", {
           flag: flagData,
           }, function (data, status) {
               callback(data);
           }
     });
 }

And then you call it like:

然后你称之为:

getDataUsers('userCount', function(data) {
      $("#hdUserCount").val(data);
      $('.loader').fadeOut('fast');
      var obj = JSON.parse($("#hdUserCount").val());
      // Add the rest of code that relies on obj .. 
}

#1


1  

That's not how asynchronous code works. This is what your function should look like:

这不是异步代码的工作原理。这是你的功能应该是这样的:

function getDataUsers(flagData, callback) {
    $('.loader').fadeIn('fast');
    $.post("Stats.ashx", {
           flag: flagData,
           }, function (data, status) {
               callback(data);
           }
     });
 }

And then you call it like:

然后你称之为:

getDataUsers('userCount', function(data) {
      $("#hdUserCount").val(data);
      $('.loader').fadeOut('fast');
      var obj = JSON.parse($("#hdUserCount").val());
      // Add the rest of code that relies on obj .. 
}