简体   繁体   中英

Asp.net MVC 5 Ajax call does not trigger method

I have problem that when I make an ajax call it does not reach the server. I've seen multiple post about this problem, but these posts dont work for me. The weird thing that occurs is that, this code is working when I use the start with debugger in firefox it also hits the breakpoint then. But when I start the project without debugger it does not work on either firfox or chrome. what is going wrong?

this is my ajax call:

$(document).ready(function () {
        var events = [];            
        $.ajax({
            type: "GET",
            url: "Agenda/GetEvents",
            success: function (data) {
                $.each(data, function (i, v) {
                    events.push({
                        title: v.Subject,
                        description: v.Description,
                        start: moment(v.StartDateTime),
                        end: v.EndDateTime != null ? moment(v.EndDateTime) : null,
                        color: v.ThemeColor,
                        allDay: v.IsFullDay
                    });
                    console.log("Pushing");
                })
                GenerateCalender(events);
            },
            error: function (error) {
                alert('failed');
            }
        })

        function GenerateCalender(events) {
            $('#calender').fullCalendar('destroy');
            $('#calender').fullCalendar({
                aspectRatio: 1.5,
                defaultDate: new Date(),
                timeFormat: 'HH:mm',
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month'
                },
                eventLimit: true,
                eventColor: '#378006',
                events: events,
                eventClick: function (calEvent, jsEvent, view) {
                    $('#myModal #eventTitle').text(calEvent.title);
                    var $description = $('<div/>');
                    $description.append($('<p/>').html('<b>Starttijd: </b>' + calEvent.start.format("DD-MMM-YYYY HH:mm ")));
                    if (calEvent.end != null) {
                        $description.append($('<p/>').html('<b>Eindtijd: </b>' + calEvent.end.format("DD-MMM-YYYY HH:mm ")));
                    }
                    $description.append($('<p/>').html('<b>Beschrijving: </b>' + calEvent.description));
                    $('#myModal #pDetails').empty().html($description);

                    $('#myModal').modal();
                }
            })
        }
    })

And this is the method it needs to go in in the AgendaController:

    [HttpGet]
    public JsonResult GetEvents()
    {
        var listofEvents = db.Events.ToList(); 

        return new JsonResult{ Data = listofEvents, JsonRequestBehavior = JsonRequestBehavior.AllowGet};
    }

Thank you in advance

[HttpGet]
public JsonResult GetEvents()
{
    var listofEvents = db.Events.ToList(); 

    return Json (Data = listofEvents, JsonRequestBehavior.AllowGet);
}

I already found the solution. The problem is not in the Ajax call... the problem was that the controller had an authorize on it and I forgot to make the GetEvents method allowanonymous. my bad sorry for wasting anyones time :') Should I delete this post? or should I take no further action?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM