Estoy tratando de mostrar datos del archivo events.json en mi sitio web. mi código muestra datos en console.log , pero ¿cómo puedo almacenarlo en variable para recuperarlo en el sitio web?
events.json file data: [ { "id": "d8jai72s", "name": "Vacations", "description": "TEST", "date": "March/16/2022", "type": "vacation" } ]Código para mostrar la respuesta en la consola:
$(document).ready(function() { fetch("events.json") .then(response => { return response.json(); }) .then(jsondata => console.log(jsondata));Quiero usar esta respuesta aquí en el mismo archivo .js:
$("#demoEvoCalendar").evoCalendar({ format: "MM dd, yyyy", titleFormat: "MM", calendarEvents: [{ > // i want place response data in calendarEvents here with other events. id: "d8jai72s", name: "Vacations", description: "Winter Vacation Starts (All Classes)", date: ["January/01/2022", "January/13/2022"], type: "vacation", everyYear: 0 } ] }); ACTUALIZACIÓN: siguiendo el comentario "Take whatever code that you want to access jsondata and move it into the .then(jsondata => { ... }) function "
Edité mi código de la siguiente manera y no funciona:
$(document).ready(function() { fetch("events.json") .then(response => { return response.json(); }) .then(jsondata => { $("#demoEvoCalendar").evoCalendar({ format: "MM dd, yyyy", titleFormat: "MM", calendarEvents: [ { id: "d8jai72s", name: "New Academic Year", description: "New Academic year starts (GR. 2 to GR. 5)", date: "April/14/2022", type: "newyear", everyYear: 0 }, ] }) });