简体   繁体   English

Asp.net MVC 5 Ajax调用不会触发方法

[英]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. 我有一个问题,当我进行ajax调用时,它无法到达服务器。 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. 发生的奇怪的事情是,当我在firefox中使用debugger开头时,此代码正在工作,然后它也会达到断点。 But when I start the project without debugger it does not work on either firfox or chrome. 但是,当我在没有调试器的情况下启动项目时,它既不能在firfox上也不能在chrome上工作。 what is going wrong? 怎么了?

this is my ajax call: 这是我的ajax电话:

$(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: 这是它需要在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. 问题不在Ajax调用中……问题在于控制器具有授权,而我忘记了让GetEvents方法允许匿名。 my bad sorry for wasting anyones time :') Should I delete this post? 我很抱歉浪费任何人的时间:')我应该删除此帖子吗? or should I take no further action? 还是不应该采取进一步措施?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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