Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery weekcalendar:无法调用方法';getTime';未定义的_Javascript_Jquery_Json - Fatal编程技术网

Javascript jQuery weekcalendar:无法调用方法';getTime';未定义的

Javascript jQuery weekcalendar:无法调用方法';getTime';未定义的,javascript,jquery,json,Javascript,Jquery,Json,我使用的不是静态数据,而是数据库中的数据。下面的脚本创建包含日历所有约会的eventData1变量。当我手工粘贴下面的输出时,一切正常。当我通过变量添加它时,它会崩溃,并显示以下错误消息: Uncaught TypeError: Cannot call method 'getTime' of undefined 因此,我认为它无法将日期转换为新的日期对象。这就是为什么我尝试使用一个单独的函数将字符串转换为日期,但没有成功 events : [{'id': 25, 'start': new D

我使用的不是静态数据,而是数据库中的数据。下面的脚本创建包含日历所有约会的
eventData1
变量。当我手工粘贴下面的输出时,一切正常。当我通过变量添加它时,它会崩溃,并显示以下错误消息:

Uncaught TypeError: Cannot call method 'getTime' of undefined 
因此,我认为它无法将日期转换为新的日期对象。这就是为什么我尝试使用一个单独的函数将字符串转换为日期,但没有成功

events : [{'id': 25, 'start': new Date( '2013-01-07 14:45:00'), 'end': new Date('2013-01-07 15:45:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 26, 'start': new Date( '2013-01-10 11:15:00'), 'end': new Date('2013-01-10 12:15:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 22, 'start': new Date( '2013-01-09 12:45:00'), 'end': new Date('2013-01-09 14:45:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 21, 'start': new Date( '2013-01-09 15:00:00'), 'end': new Date('2013-01-09 17:00:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 20, 'start': new Date( '2013-01-08 16:00:00'), 'end': new Date('2013-01-08 17:00:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 27, 'start': new Date( '2013-01-10 15:45:00'), 'end': new Date('2013-01-10 16:45:00'), 'title': 'test appointment from javascript', userId: 0}]
这是我的JavaScript:

  <script type="text/javascript">

      function dateReviver(value) {
        if (typeof value === 'string') {
          var re = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/
          var result = re.exec(value);
          if (result) {
              return new Date(Date.UTC(+result[1], +result[2] - 1, +result[3], +result[4],+result[5], +result[6]));
          }
        }
        return value;
      }

      var appointments = "[";
      var eventData1 = new Array();
      var counter = 0;

      $.ajax({
        url: 'http://www.slinder.ch/admin/php/termin_getappointments.php',
        dataType: 'json',
        async: false,
        success: function(data) {

            console.log('entered getJSON()');
            console.log(data);
            $.each(data, function(i, appointment) {
                var id = appointment.appointmentId;
                var start = appointment.start;
                var end = appointment.end;
                var title = appointment.prename;
                var userid = appointment.workerid;

                appointments += "{'id': " + id + ", 'start': new Date( '" + start + "'), 'end': new Date('" + end + "'), 'title': 'test appointment from javascript', userId: 0}";

                console.log(start);
                console.log(end);

                if (i === (data.length - 1)) {
                    // this is the last                    
                } else {
                    appointments += ",";
                }             
                counter++;
            });

            appointments += "]";
            console.log('first: ' + appointments);

            eventData1 = {
                options: {
                  timeslotsPerHour: 4,
                  timeslotHeight: 20,
                  defaultFreeBusy: {free: true}
                },
                events : appointments,

                freebusys: []
            };
        }
  });

  </script>

有一个类似的示例,但也没有经过批准的答案。

您正在
约会
变量中构建一个字符串。因此,它不能用作数组。相反,用对象构建一个普通的JavaScript数组

var appointments = [];
....
appointments.push({
    id: id,
    start: new Date(start),
    end: new Date(end), 
    title: 'test appointment from javascript', 
    userId: 0
});
...
编辑:


或者,您可以简单地推送从服务器获得的数据,因为它看起来不像您实际在修改它。如果只是简单地重命名键,那么可能不值得付出额外的努力。

为什么要通过字符串连接构建JSON?只需构建一个对象并使用
JSON.stringify
var appointments = [];
....
appointments.push({
    id: id,
    start: new Date(start),
    end: new Date(end), 
    title: 'test appointment from javascript', 
    userId: 0
});
...