Calling jQuery.ajax or jQuery.post from inside one of the event callbacks results in a type error from Moment.min.js

I am working on a schedule using FullCalendar and I hit a bit. I am trying to use event callback functions to create a new event and send new event information to my php script, which then stores the information on the server. But whenever I try to call $ .ajax or $ .post from one of the callbacks (and I tried if there are several of them in them), I get the following:

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 since I used Moment.min.js, it was hard to read where the problem came from, so I replace Moment.min.js with Moment.js, and when the error popped up again, I was able to read where the problem is:

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

From what I compiled, m is undefined by the time this function is executed. Here is the code I used to post the new event data in a 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 }, }); }, 
+8
ajax callback momentjs typeerror fullcalendar
source share
3 answers

I assume start is a target object.

I had the same problem and it seems to me: you cannot send the target 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

+13
source share

Do not send start and end directly as AJAX request parameters.

 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); }); 
+6
source share

I had the same problem (as I came across this post).

To send the whole object, you need to strengthen it.

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

All Articles