Skip to content

Instantly share code, notes, and snippets.

@LarryBarker
Created August 6, 2020 14:01
Show Gist options
  • Save LarryBarker/dfbe62eb1bf7787753c69d87b4cfe3ca to your computer and use it in GitHub Desktop.
Save LarryBarker/dfbe62eb1bf7787753c69d87b4cfe3ca to your computer and use it in GitHub Desktop.
Custom request object for October-based SPA
export const request = async (handler, data) => {
let response = await fetch('/api', {
method: 'POST',
credentials: 'same-origin',
body: JSON.stringify(data),
cache: 'no-cache',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json',
'X-OCTOBER-REQUEST-HANDLER': handler,
'X-OCTOBER-REQUEST-PARTIALS': ''
},
})
if (response.ok) { // if HTTP-status is 200-299
return await response.json()
} else {
alert('HTTP-Error: ' + response.status);
}
}
async sayHello() {
this.message = await this.fetchAsync('onSayHello', { name: this.name })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment