【问题标题】:Calling JQuery.ajax or JQuery.post from inside of one of the event callbacks results in a Type Error from Moment.min.js从事件回调之一内部调用 JQuery.ajax 或 JQuery.post 会导致 Moment.min.js 出现类型错误
【发布时间】:2014-11-09 17:20:48
【问题描述】:

我正在使用 FullCalendar 制定时间表,但遇到了一些障碍。我正在尝试使用事件回调函数来创建一个新事件,并将新事件的信息发送到我的 php 脚本,然后将信息存储到服务器。但是,每当我尝试从其中一个回调中调用 $.ajax 或 $.post 时(我已经尝试过其中一些回调中的 if),我都会得到:

Uncaught TypeError: Cannot read property 'month' of undefined moment.js:684
extend.monthsShort moment.js:684
e jquery.min.js:4
Bc jquery.min.js:4
Bc jquery.min.js:4
Bc jquery.min.js:4
n.param jquery.min.js:4
n.extend.ajax jquery.min.js:4
$.fullCalendar.select advisorSchedule.js:141
Q fullcalendar.min.js:6
s fullcalendar.min.js:7
a fullcalendar.min.js:7
(anonymous function) fullcalendar.min.js:6
d jquery.min.js:3
n.event.dispatch jquery.min.js:3
r.handle

但是因为我使用的是 Moment.min.js,所以很难看出问题出在哪里,所以我用 Moment.js 替换了 Moment.min.js,当错误再次出现时,我能够阅读问题出在哪里:

months : function (m) {
   return this._months[m.month()];
},

根据我收集的信息,在执行此函数时,m 是未定义的。 这是我用来将新事件数据发布到 php 脚本的代码:

select:function(start, end, jsEvent, view){
        CURRENT_SELECTION_START = start;
        CURRENT_SELECTION_END   = end;

        console.log(CURRENT_SELECTION_START + '-' + CURRENT_SELECTION_END);

        $.ajax({

            url:'../AJAX/save_event.php',
            type: 'POST',
            data: {
                Start_Time: start,
                Event_Title: prompt('Title'),
                End_Time: end
            },

        });
    },

【问题讨论】:

    标签: ajax callback fullcalendar typeerror momentjs


    【解决方案1】:

    我假设 start 是一个时刻对象。

    我遇到了同样的问题,似乎:您无法发送时刻对象,因此您需要以整数或其他形式获取值。

    试试start.calendar()start.format("YYYY-MM-DD");(或类似的东西)而不是start

    为我工作,希望也为你工作;)

    帕特里克

    【讨论】:

      【解决方案2】:

      我刚刚遇到了同样的问题(这就是我看到这篇文章的方式)。

      要发送整个对象,您需要对其进行字符串化。

      var now = moment();
      
      $.ajax({
            url: 'http://example.com',
            contentType: 'application/json',
            data: JSON.stringify(now),
            type: 'POST'
      });
      

      【讨论】:

        【解决方案3】:

        不要将startend直接作为参数发送到AJAX请求中。

        var startDate = start.format('YYYY-MM-DD'),
            endDate = end.format('YYYY-MM-DD'),
            params = {'from':startDate, 'to':endDate};
        $.ajax({
              url: 'http://example.com',
              contentType: 'application/json',
              data: params,
              type: 'POST'
            }).done(function(data) {
                console.log('Successful! ', data);
            });
        

        【讨论】:

          猜你喜欢
          • 2013-12-18
          • 1970-01-01
          • 2023-04-07
          • 2014-03-14
          • 2020-05-29
          • 1970-01-01
          • 1970-01-01
          • 2013-06-07
          • 2012-09-03
          相关资源
          最近更新 更多