This is a snippet of JavaScript code that deals with setting up cookies and submitting a form.
import Helper from './helper'
import List from './itemList'
let myForm = document.getElementById('myForm');
function getAlfrescoTicket(e) {
e.preventDefault(); //required for setting cookie
let auth = {};
auth.username = this.username.value;
auth.password = this.password.value;
let xhr = Helper.getXmlHttp();
xhr.open("POST", List.ticketURL);
xhr.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
Helper.setCookie('ticket',
JSON.parse(this.responseText).data.ticket, 1);
console.log(Helper.getCookie('ticket'));
}
};
xhr.send(JSON.stringify(auth));
}
myForm.addEventListener('submit', getAlfrescoTicket);
The issue being faced here is when using e.preventDefault() to set cookies, the form does not submit and the page does not change. But if e.preventDefault() is removed, the form works fine and the page changes without setting up cookies. The challenge now is how to overcome this hurdle and have both cookies set up while also allowing the page to load properly.