Created
September 13, 2019 14:53
-
-
Save jfolds/dd2730ef4a969e6b75edf4a0990bed14 to your computer and use it in GitHub Desktop.
API request with credentials via fetch/xhr/jquery
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
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