-
-
Save oralunal/b24ec9001ef0e452c750b3730679188c 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