Skip to content

Instantly share code, notes, and snippets.

@jfolds
Created September 13, 2019 14:53
Show Gist options
  • Save jfolds/dd2730ef4a969e6b75edf4a0990bed14 to your computer and use it in GitHub Desktop.
Save jfolds/dd2730ef4a969e6b75edf4a0990bed14 to your computer and use it in GitHub Desktop.
API request with credentials via fetch/xhr/jquery
const apiBaseUrl 'https://localhost/some/api';
/** fetch */
window.fetch(`${apiBaseUrl}`, {
credentials: 'include'
})
.then(json)
.then((data) => {
console.log(data);
});
/** XHR */
// const xhr = new window.XMLHttpRequest();
// xhr.withCredentials = true;
// xhr.responseType = 'json';
// xhr.addEventListener('readystatechange', () => {
// if (xhr.readyState === 4) {
// setUserInfo(xhr.response);
// }
// });
// xhr.open('GET', `${apiBaseUrl}/values`);
// xhr.send(null);
/** jquery */
// $.ajax({
// url: `${apiBaseUrl}`,
// xhrFields: {
// withCredentials: true
// }
// })
// .then((res) => {
// setUserInfo(res);
// console.log('success', res);
// })
// .fail((err) => {
// console.log('error', err);
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment