Calling JQuery.ajax or JQuery.post from inside of one of the event callbacks results in a Type Error from Moment.min.js

☆樱花仙子☆ 提交于 2019-11-27 06:46:51

问题


I am working on a schedule using FullCalendar and i have hit a bit of a snag. I am trying to use the event callback functions to create a new event, and send the new event's information to my php script which then stores the information to the server. But whenever i try to call $.ajax or $.post from within one of the callbacks (and i have tried if from with in a few of them) i get this:

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

But because i was using Moment.min.js it was kinda hard to read where the problem was coming from, so i replaces Moment.min.js with Moment.js, and when the error popped up again i was able to read where the problem was:

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

From what i gathered, m is undefined by the time this function is executed. Here is the code i used to post the new event data to the php script:

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
            },

        });
    },

回答1:


I assume start is a moment-object.

I had the same problem and it seems: you can't send the moment-object, so you need to get the value as an integer or something else.

Try start.calendar() or start.format("YYYY-MM-DD"); (or something similar) instead of start.

worked for me, hopefully for you too ;)

Patrick




回答2:


I just had this exact same problem (it's how I came across this post).

To send the whole object you need to stringify it.

var now = moment();

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



回答3:


Don't send start and end directly as the parameters to the AJAX request.

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);
    });


来源:https://stackoverflow.com/questions/24594685/calling-jquery-ajax-or-jquery-post-from-inside-of-one-of-the-event-callbacks-res

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!