当我尝试将数据推送到数组中时,我在数组中未定义

问题描述 投票:0回答:2

请谁能帮我解决这个问题? 我已经尝试解决这个问题 3 天了,jquery Ajax 对我来说是个新手。

我想使用 jquery Ajax 将实时数据与 asp.net core 6.0 中的日历绑定。 我在 jquery Ajax 的成功结果中从数据库中获取了数据。

当我尝试将来自数据库的数据推送到数组中时出现错误。 当我试图将数据推送到数组中时,我在数组中得到了未定义。 我不知道我哪里做错了。

$(document).ready(function() {
  debugger
  $.ajax({
    type: "GET",
    url: "/Admin/Calendar/GetBookings",
    dataType: "JSON",
    success: function(result) {
      debugger
      var Booking = [];
      $.each(result, function(i, data) {
        Booking.push({
          title: data.title,
          description: data.description,
          start: data.startDate,
          end: data.endDate
        });
      });
      GenerateCalendar(Booking);
    },
    error: function(error) {
      alert('failed');
    }
  });
  function GenerateCalendar(Booking) {
    debugger
    $('#calendar').fullCalendar({
      header: {
        left: 'prev,next,today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
      }
    })
  }
});
javascript jquery arrays asp.net-core calendar
2个回答
1
投票

结果不是一个数组,而是一个包含数组的对象。

代替:

$.each(result, function(i, data)

你应该使用:

$.each(result.data, function(i, data)


0
投票

假设你有 result.data 作为一个数组,你可以这样做

const Booking = result.data
  .map(({title,description,startDate,endDate}) => ({ title, description, start:startDate, end: endDate });
© www.soinside.com 2019 - 2024. All rights reserved.