I am developing a component and I use ajax calls to do some tasks in the frontend. I have come to a problem when the session is expires and I don't want to use keepalive to solve it.
In the frontend I have a list view. And there there are some buttons that perform tasks using ajax requests.
For example :
$('#resetData').on('click', function (e) {
e.preventDefault();
var token = jQuery("#token").attr("name");
var itemId = $(this).attr('data-id');
$.ajax({
method: 'POST',
cache: false,
dataType: "json",
data: { [token]: "1", id: itemId},
url: 'index.php?option=com_contract&task=contract.resetData'
}).success(function (r) {
console.log('Response :');
console.log(r);
if (r.responseText) {
UIkit.modal.alert(r.responseText).then(function () {
window.location.reload();
});
}
if (r.message) {
UIkit.modal.alert(r.message).then(function () {
window.location.reload();
});
}
}).error(function (err) {
console.log(err);
UIkit.modal.alert('<div class="alert alert-error">' + err + '</div>').then(function () {
return;
});
}).complete(function (r) {
console.log(r.responseText);
});
When the session has expired, in my main controller I redirect to login page.
JFactory::getApplication()->redirect(JURI::base() . 'index.php?option=com_users&view=login', $error, 'error'); //redirect to login page
This means that the hole html is returned in the Ajax response and when I popup the response I get an [object object]
instead of a human readable error.
I guess I need to find a way to check if the session has expired when click
event fires and alert an error, before I make the Ajax call.
Is it possible to check the JSESSION in javascript or any other way to solve it ?