How to get ajax call return code when getting fullcalendar events

179 Views Asked by At

I have this fullcalendar definition:

            $('[data-toggle="calendar"]').fullCalendar({
                    themeSystem: 'bootstrap4',
                    locale: 'es',
                    monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
                    monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
                    dayNames: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
                    dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mié', 'Jue', 'Vie', 'Sáb'],
                    header: {
                        left: 'title',
                        center: '',
                        right: 'prev,next today'
                    },
                    buttonText: {
                        today: 'Hoy',
                        month: 'Mes',
                        week: 'Semana',
                        day: 'Día'
                    },
                    displayEventTime: false,
                    events: '/myURL',
                    eventRender: function (eventObj, $el) {
                        $el.popover({
                            title: 'Menú ' + eventObj.title,
                            content: eventObj.description.replace(/\n/g, '<br>'),
                            trigger: 'hover',
                            placement: 'top',
                            container: 'body',
                            html: true
                        });
                    },
                });

Note the events definition. It calls a method using an ajax call. The problem here is that the ajax call can return a forbidden error code. When that condition occurs, that error code is sent using the response header (X-Responded-JSON).

I need to be able to do something like this:

function checkUserSession(xhr) {
    // Si es 401, es porque se debe redireccionar al login
    var response = xhr && typeof xhr.getResponseHeader === 'function' ? xhr.getResponseHeader("X-Responded-JSON") : null;
    if (response !== null) {
        var obj = JSON.parse(response);
        if (obj.status === 401) {
            //var href = obj.headers.location;
            //href = obj.headers.location.substr(0, obj.headers.location.indexOf('?ReturnUrl=') + 11) + '/' + location.pathname;
            redirectToLogin();
            return false;
        }
    }

    return true;
}

That is, I need to get the returned XHR object from the ajax call. I do this when I do other ajax calls but I have not found a way to do this in fullcalendar plugin.

Regards Jaime

0

There are 0 best solutions below