Skip to content

Instantly share code, notes, and snippets.

@sunny
Created February 28, 2022 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunny/e510ea2511822afba35d01d1c36b7f6c to your computer and use it in GitHub Desktop.
Save sunny/e510ea2511822afba35d01d1c36b7f6c to your computer and use it in GitHub Desktop.
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