FullCalendar experiencing issues displaying some events when using AJAX/JSON

I've been experimenting with FullCalendar for some time now, but I'm facing a minor issue. When I load events through AJAX/JSON in the correct format, the calendar only displays the first event.

Although all other events are visible in the console.log output, they do not appear on the calendar. The sample event code from the documentation looks like this:

events: [{
    title: 'My Event',
    start: '2010-01-01',
    url: 'http://google.com/'
  },
  {
    title: 'My Event',
    start: '2010-01-02',
    url: 'http://google.com/'
  }
  // other events here
],

Here's my code snippet:

events: function(start, end, timezone, callback) {
  function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
  }
  $.ajaxSetup({
    beforeSend: function(xhr, settings) {
      if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
        xhr.setRequestHeader("X-CSRFToken", csrftoken);
      }
    }
  });
  $.ajax({
    url: "/tande/fetch_holidays/",
    type: 'POST',
    success: function(data) {
      var events = [];
      $.map(data.holidays, function(value) {
        console.log(value)
        description = value.toString().split('_')[0]
        start_date = value.toString().split('_')[1]
        end_date = value.toString().split('_')[2]
        person = value.toString().split('_')[3]
        events.push({
          title: person,
          start: start_date,
          end: end_date
        });
        callback(events);
        console.log(events);
      })
    },
  })
},

Answer №1

There was an issue with the placement of the callback for events in the code. The corrected version is provided below:

events: function(start, end, timezone, callback) {
  function csrfSafeMethod(method) {
    // Ensure these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
  }
  $.ajaxSetup({
    beforeSend: function(xhr, settings) {
      if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
        xhr.setRequestHeader("X-CSRFToken", csrftoken);
      }
    }
  });
  $.ajax({
    url: "/tande/fetch_holidays/",
    type: 'POST',
    success: function(data) {
      var events = [];
      $.map(data.holidays, function(value) {
        console.log(value)
        description = value.toString().split('_')[0]
        start_date = value.toString().split('_')[1]
        end_date = value.toString().split('_')[2]
        person = value.toString().split('_')[3]
        events.push({
          title: person + ' - ' + description,
          start: start_date,
          end: end_date,
        });
      })
      callback(events);
    },
  })
},

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps are needed to craft a customized greeting on discord using the latest nodejs update?

I am facing an issue where the following code is not sending anything: client.on("guildMemberAdd", (member) => { const welcomeMessage = "Please give a warm welcome to <@${member.id}> <a:pepesimp:881812231208181790> as th ...

Difficulties encountered when trying to interact with buttons using JS and Bootstrap

I'm working with a Bootstrap 5 button in my project and I want to access it using JavaScript to disable it through the attribute. Here is the code I've been testing: Script in Header: ` <script> console.log(document.getElementsByName(& ...

Display the initial three image components on the HTML webpage, then simply click on the "load more" button to reveal the subsequent two elements

I've created a div with the id #myList, which contains 8 sub-divs each with an image. My goal is to initially load the first 3 images and then have the ability to load more when clicking on load more. I attempted to follow this jsfiddle example Bel ...

Exploring querying techniques in Node.js

Hi there, I'm new to asking questions and I'm really hoping to find some answers here. I am just starting out with JavaScript so please explain things in simple terms. So here's my issue - I'm not sure how to properly query. - Should ...

What is the best way to create a gradual color change in each individual cell instead of changing all at once?

Looking for some help with a grid I'm working on. I've got the cells changing colors like I want them to, but they're all changing at once. What I really need is for them to light up in a specific order - Yellow, Green, Blue, White, Orange. ...

Implementing knockoutjs re-bind when the document is reloaded via AJAX requests

I am currently using a mobile UI Framework that loads pages via ajax. When changing a page, it removes the DOM of the previous page using $.remove(); In addition to that, I rely on knockoutjs to bind data on every page. The issue arises when page A is re ...

Oops! Module './api/routers' not found

Hello, I hope everyone is doing well... Currently, I am working on developing a NextJS single-page web application. To create a custom server for NextJs, I am utilizing Express, MongoDB, and nodemon for hot reload functionality. Upon starting the server, ...

JavaScript - Error: The '}' token was unexpected

Every time I attempt to move <div id="test">this should go down</div> downwards using: <div onclick="(".test").slideDown(800);">close it</div> I encounter an error whenever I click 'close it' SyntaxError: Unexpected to ...

Why does everything seem to move in sync with the table's scrolling?

I recently had an issue resolved that fixed most of my concerns: I needed to make three separate edits, and now when I reintroduce the other two edits, they are included within the table section and scroll along with the table instead of being stuck beneat ...

Align list items in the center horizontally regardless of the width

I have created this container element: export const Container = styled.section<ContainerProps>` display: flex; justify-content: center; `; The current setup is vertically centering three list items within the container. However, I am now looking ...

Error encountered when simulating the effect of calling 'insertPlayerData' function: ReferenceError - currentUserId has not been defined

PlayerList = new Mongo.Collection('players'); UerAccounts = new Mongo.Collection('user'); if(Meteor.isClient){ Template.leaderboard.helpers({ 'player':function(){ var currentUserId = Meteor.userId(); return Play ...

What is the procedure for manipulating a string within an array of objects with a string key-value pair using underscore.js?

I am seeking a solution utilizing underscores, but I am open to a vanilla JS alternative if it proves to be the most effective option. My goal is to utilize array 2 to modify strings in the objects of array1 that either start with or end with the strings ...

How can I extract a substring from a URL and then save it to the clipboard?

Hi there! I'm working on a project for my school and could really use your expertise. I need help extracting a substring from a URL, like this one: URL = https://www.example.com/blah/blah&code=12432 The substring is: 12432 I also want to display ...

Is there a way to create triangles on all four corners of an image using canvas in JavaScript?

In my code, I am currently working on drawing rectangles on the corners of an image using JavaScript. To achieve this, I first extract the image data from a canvas element. // Get image data let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height) ...

select items using a dropdown menu in an Angular application

Let me describe a scenario where I am facing an issue. I have created an HTML table with certain elements and a drop-down list Click here for image illustration When the user selects in, only records with type in should be displayed Another image refere ...

What is the process for sending a put request in a Ruby on Rails application using AJAX?

I've been searching online for a solution, but I can't seem to find the right setup. After trying different things, this is what I have come up with: var dataString = $('#markerform').serialize(); var id = $('#route_id&apo ...

Styling the "Browse" button for different web browsers

Here is the code snippet I am working with: HTML: <form> <input id = "file" type="file" /> <div id="custom_button">custom button</div> </form> Jquery: $("#custom_button").on("click", function () { $("#file"). ...

Most efficient method to send an email through a pre-existing form on a website

After setting up all the HTML files for the site, I found that using jQuery mobile was more effective than ColdFusion. However, changing the filetypes from .html to .cfm created a lot of issues. Now, I just need to validate a couple fields and email the da ...

How can I pass a specific value, rather than the entire array, in a ReactJS dropdown menu?

I am facing a problem where the entire array is being passed as an argument when I call onchange after getting correct values in the dropdown. Instead of only receiving the selected value, e contains the whole array. Here is the code snippet that demonst ...

"Creating a fixed header with jQuery by manipulating the offset and scrollTop

My goal is to create a smooth scrolling website that scrolls vertically using jQuery. I found a helpful tutorial called Smooth Scrolling Website and have been following it closely to build my site. However, I'm facing an issue with a fixed header - t ...