web/assets/js/home.js
2018-05-16 23:01:06 -04:00

52 lines
1.2 KiB
JavaScript

function getCookie(name) {
if (!document.cookie) {
return null;
}
const xsrfCookies = document.cookie.split(';')
.map(c => c.trim())
.filter(c => c.startsWith(name + '='));
if (xsrfCookies.length === 0) {
return null;
}
return decodeURIComponent(xsrfCookies[0].split('=')[1]);
}
function contactRequest() {
var name = document.querySelector('input[name=name]').value
var email = document.querySelector('input[name=email]').value
var success = document.querySelector('div.alert-success')
var failure = document.querySelector('div.alert-error')
// Reset response holders
success.innerHTML = ''
failure.innerHTML = ''
fetch("contact/request", {
body: JSON.stringify({
name: name,
email: email
}),
headers: new Headers({
'Content-Type': 'application/json',
'X-XSRF-TOKEN': getCookie('_csrf')
}),
method: 'POST'
})
.then(function(resp) {
if(resp.ok) {
success.innerHTML = 'Request Sent'
return
}
return resp.json()
})
.then(function(o) {
failure.innerHTML = o.message
})
.catch( function(err) {
failure.innerHTML = 'network error'
})
}