im trying to achieve something pretty simple, i just wanna post the form to itself without a refresh, however i require it to post the data from the form as it is captured by the controller. im getting the data in the back end i just need the form to post successfully without a refresh and then show and hide certain things. Also, i get this error
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
i presume this is why im not reaching my conditional
const wrappers = document.querySelectorAll('.form-wrapper');
if (wrappers) {
wrappers.forEach(function(el){
const form = el.querySelector('.submit-form');
let currentURL = window.location.href;
const output = el.nextElementSibling;
form.addEventListener('submit', function(e){
const data = new FormData(form);
fetch(`${currentURL}`, {
credentials: 'include',
method: 'POST',
body: data,
})
.then((res) => res.json())
.then((res) => {
if (res.success) {
console.log('submitted');
output.classList.remove('none');
form.classList.add('none');
}
else if (res.error) {
console.log('Error: ' + res.error);
}
});
e.preventDefault();
},false);
});
}