Created
August 6, 2020 14:01
-
-
Save LarryBarker/dfbe62eb1bf7787753c69d87b4cfe3ca to your computer and use it in GitHub Desktop.
Custom request object for October-based SPA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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