Created
February 28, 2022 15:14
-
-
Save sunny/e510ea2511822afba35d01d1c36b7f6c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const htmlFetch = async (url, { headers, body, ...options } = {}) => { | |
const token = document.querySelector('meta[name="csrf-token"]').content; | |
options = { | |
headers: new Headers({ | |
Accept: "text/html", | |
"Content-Type": "application/json", | |
"X-Requested-With": "XMLHttpRequest", | |
"X-CSRF-Token": token, | |
...headers, | |
}), | |
credentials: "same-origin", | |
body: body && JSON.stringify(body), | |
...options, | |
}; | |
const response = await fetch(url, options); | |
return response.ok ? response.text() : Promise.reject(response); | |
}; | |
export { htmlFetch }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment