【问题标题】:jquery undefined error for simple ajax request简单ajax请求的jquery未定义错误
【发布时间】:2017-06-24 07:32:34
【问题描述】:

我已经包含了 jquery,然后所有 ui js 文件作为少数线程表明此错误是由于依赖关系。用户上传照片时使用canvasjs绘制图形。

  Uncaught TypeError: Cannot read property 'getTime' of undefined

包含所有必需依赖项的引导 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);
            }
        })
    })();
});

【问题讨论】:

  • sthtml有什么数据???
  • @Dinesh 响应 json 数据为 { x: 1, y: 450 },{ x: 2, y: 414} 相似类型
  • @Dinesh 感谢您的回答。我认为这是正确的方法,因为我现在没有收到错误,但是 json 正在被网格化,所以图表没有显示。控制台日志显示这样的数据 ["{ x: 3, y: 33.6 },", "{ x: 4, y: 62.2 },", "{ x: 5, y: 121.2 },", "", x: 0] 我不知道“,”是怎么来的,我的 json 输出只是 { x: 3, y: 33.6 } 这样。我已经更改了我的 json 并删除了,所以 jquery 将添加逗号,但我怎样才能避免 [ 和 "
  • 你控制服务器端代码吗? json格式很不对。
  • @Dinesh 我控制服务器端代码,但这是根据 canvasjs 数据格式的要求

标签: javascript jquery ajax twitter-bootstrap canvasjs


【解决方案1】:

你的 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);
            }
        })
    })();
});

【讨论】:

  • 感谢您的回答。我认为这是正确的方法,因为我现在没有收到错误,但是 json 正在被网格化,所以图表没有显示。控制台日志显示这样的数据 ["{ x: 3, y: 33.6 },", "{ x: 4, y: 62.2 },", "{ x: 5, y: 121.2 },", "", x: 0] 我不知道“,”是怎么来的,我的 json 输出就是 { x: 3, y: 33.6 }like that
猜你喜欢
  • 1970-01-01
  • 2013-04-12
  • 2016-10-05
  • 2012-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-13
相关资源
最近更新 更多