Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oralunal/b24ec9001ef0e452c750b3730679188c to your computer and use it in GitHub Desktop.
Save oralunal/b24ec9001ef0e452c750b3730679188c 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