无法读取属性&has;'未定义的

时间:2016-01-26 07:59:35

标签: javascript jquery laravel fullcalendar laravel-5.1

我使用FullCalendar在日历上显示工作时间。

我通过像这样的ajax调用来拉动事件:

"events": function(start, end, timezone, callback) {

  //create the data to be sent
  var objectToSend = {
    "start_date": start.format("YYYY-MM-DD"),
    "finish_date": end.format("YYYY-MM-DD"),
  };

  //craft and make the request
  $.ajax({
    url: 'calendar/test',
    data: objectToSend,
    type: 'POST',
    cache: false
  }).done(function(data) {
    //on success call `callback` with the data
    callback(data)
  })
}

这非常合适,但是我在控制台中显示错误" Uncaught TypeError:无法读取属性' hasTime'未定义"而且这来自fullcalendar.min.js:6

我的JavaScript不是很流利,但我的搜索表明我还没有提供right datesjunk data

据我所知,我提供了所有正确的数据。生成数据的函数如下所示:

public function test(Request $request) {
  $start_date = Input::get('start_date');
  $finish_date = Input::get('finish_date');

  $shifts = Roster::whereBetween('date', array($start_date, $finish_date)) - > get();

  foreach($shifts as $shift) {
    $start = $shift - > date.
    ' '.$shift - > start_time;
    $finish = $shift - > date.
    ' '.$shift - > finish_time;

    $events[] = array(
      'title' => $shift - > staff - > first_name,
      'start' => Carbon::createFromFormat('Y-m-d H:i:s', $start) - > toDateTimeString(),
      'end' => Carbon::createFromFormat('Y-m-d H:i:s', $finish) - > toDateTimeString(),
      'id' => $shift - > id,
      'allDay' => false
    );
  }

  return json_encode($events);
}

输出:

[{"title":"Gemma","start":"2016-02-01 18:00:00","end":"2016-02-01 22:00:00","id":1,"allDay":false},
{"title":"Gemma","start":"2016-01-26 18:00:00","end":"2016-01-26 22:00:00","id":49,"allDay":false}]

有人能发现我做错了吗?我只是想用它来渲染给定月份的事件。

编辑:console.log(数据)的输出

打印出来:

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

打开这个我得到:

0: Object

打开这个我得到:

allDay: false
end: "2016-02-01 22:00:00"
id: 1
start: "2016-02-01 18:00:00"
title: "Gemma"

4 个答案:

答案 0 :(得分:2)

似乎您正在给fullCalendar错误事件的参数

首先尝试渲染一些手动事件。

events: [{
  title: 'event1',
  start: '2010-01-01'
}, {
  title: 'event2',
  start: '2010-01-05',
  end: '2010-01-07'
}, {
  title: 'event3',
  start: '2010-01-09T12:30:00',
  allDay: false // will make the time show
}]

之后,请确保您的事件与fullCalendar预期参数匹配。

答案 1 :(得分:1)

我无法弄清楚上面的代码出了什么问题,但是我通过使用JSON提示来解决它:

events: {
            url: 'calendar/test',
            error: function() 
            {
                alert("error");
            },
            success: function()
            {
                console.log("successfully loaded");
            }
        }

答案 2 :(得分:0)

我遇到了这个错误,使用了以下功能

var postToServerAjax = function(event, delta, revertFunc)
    {
      $.post('/url', {event: event}, function(data)
      {

      }, 'json').fail(function()
      {
        revertFunc();
        alert('We got an error.');
      });
    };

我发现这是因为我试图将事件传递给post()函数。如果我更改名称并不重要,一旦我通过它,我认为它试图序列化它,它导致了这个错误。现在我手动指定和对象,我“克隆”相关的id,开始和结束,因为我不需要任何其他东西。

答案 3 :(得分:0)

使用:JSON.parse(data)响应ajax。