0

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

jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • 403 Forbidden should be set as a standard HTTP status code in the response, not as some bespoke HTTP header. And 401 is "Unauthorised", not "Forbidden". Anyway it sounds like you need the [events-as-a-function](https://fullcalendar.io/docs/v3/events-function) pattern, so you can make a custom AJAX request to the server, handle the response yourself and then deal with these permission errors, but also be able to pass the events back to fullCalendar if successful. Either that, or just make your users log in before they view the calendar page at all? – ADyson Apr 29 '21 at 23:03
  • Since this is an Ajax call, all errors are returnerd using an XHR object. That 403 error is called automatically by the framwork when the user needs to login after some time. That is exactly what I need to detect in order to redirect to login page. – jstuardo Apr 30 '21 at 15:06
  • You're getting the status from the X-Responded-JSON header though in that code, not from the actual raw HTTP status. Are you in a situation like the one in this question: https://stackoverflow.com/questions/20662608/always-success-on-ajax-post-with-httpresponsemessage-401/43525824 where there's a redirect to a login page and therefore the raw status is actually 200? – ADyson Apr 30 '21 at 15:16
  • Anyway to get the XHR object, you can either follow my first suggestion above and make your own AJAX request to the server, or, fullCalendar 3 uses jQuery $.ajax underneath to make the AJAX request when you use events-as-JSON. So as per https://fullcalendar.io/docs/v3/events-json-feed you can define the "error" and "sucess" callbacks in your event options, giving you access to the underlying $.ajax error and sucess callbacks. And as per https://api.jquery.com/jQuery.ajax/ these give you access to the jqXHR object (which is a superset of the standard XHR object). – ADyson Apr 30 '21 at 15:19

0 Answers0