简单的ajax请求的jquery undefined错误

时间:2017-06-24 07:32:35

标签: javascript jquery ajax twitter-bootstrap canvasjs

我已经包含了jquery,然后是所有ui js文件,因为很少有线程表明这个错误是由于依赖性造成的。用户上传照片时使用canvasjs绘制图形。

  Uncaught TypeError: Cannot read property 'getTime' of undefined

Bootstrap html包含所有必需的依赖项。我认为某些循环问题或变量没有正确传递,但是console.log根据canvasjs.的所需数据点显示有效的json。如果这些点直接嵌入到数据点图中,则生成时没有任何错误或问题。

我也尝试更新canvasjs的功能但是不起作用,我认为这不是必需的,因为这些数据成功传递,因此没有传递空白数据。我怀疑for循环是罪魁祸首,但不知道如何以及为什么

$(document).ready(function() {
    (function() {

        $('#upload-form2').ajaxForm({
            dataType: 'json',
            success: function(data) {
               var sthtml;              
                var html = '',
                    downlo;
                for (var i = 0; i < data.length; i++) {
                    z = i + 1;
                    downlo = data[i];
                    html += '<tr id="' + i + '"><td>' + z + '</td></tr>';
                sthtml += downlo.stchart;

                }

// canvas graph starts here

        var chart = new CanvasJS.Chart("chartContainer",
    {     
      theme:"theme2",
      title:{
        text: "Game of Thrones, Viewers of the first airing on HBO"
      },
      data: [
      {        
        type: "spline",        
       dataPoints: [sthtml]
// this is the culprit dont know why but console.log shows correct canvasjs.min.js:103 Uncaught TypeError: Cannot read property 'getTime' of undefined
    //         dataPoints: [{ x: 3, y: 150 },{ x: 2, y: 14}]     
 //gives no error however my console.log output is exactly same in last line of this script 
 },
      {        
        type: "spline", 
       dataPoints: [{ x: 1, y: 450 },{ x: 2, y: 414}] 
      } 
      ],
    });
chart.render();

// canvas graph ends here

//console.log(sthtml);
            }
        })
    })();
});

1 个答案:

答案 0 :(得分:0)

你的sthtml应该是数组类型。您已经使用sthtml连接操作将sthtml转换为字符串。你需要使用数组推送操作来推送。

$(document).ready(function() {
    (function() {

        $('#upload-form2').ajaxForm({
            dataType: 'json',
            success: function(data) {
               var sthtml=[];// sthhtml is array              
                var html = '',
                    downlo;
                for (var i = 0; i < data.length; i++) {
                    z = i + 1;
                    downlo = data[i];
                    html += '<tr id="' + i + '"><td>' + z + '</td></tr>';
                sthtml.push(downlo.stchart); // use push to push objects

                }

// canvas graph starts here

        var chart = new CanvasJS.Chart("chartContainer",
    {     
      theme:"theme2",
      title:{
        text: "Game of Thrones, Viewers of the first airing on HBO"
      },
      data: [
      {        
        type: "spline",        
       dataPoints: sthtml
// this is the culprit dont know why but console.log shows correct canvasjs.min.js:103 Uncaught TypeError: Cannot read property 'getTime' of undefined
    //         dataPoints: [{ x: 3, y: 150 },{ x: 2, y: 14}]     
 //gives no error however my console.log output is exactly same in last line of this script 
 },
      {        
        type: "spline", 
       dataPoints: [{ x: 1, y: 450 },{ x: 2, y: 414}] 
      } 
      ],
    });
chart.render();

// canvas graph ends here

//console.log(sthtml);
            }
        })
    })();
});