Created
November 18, 2017 05:24
-
-
Save s-c-p/649afac3d7e3c5a8a1ba7049a8fa6372 to your computer and use it in GitHub Desktop.
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
localStorage.jwt = 'header.payload.signature'; | |
let auth_fetch = function(api_url, token) { | |
let myHeader = new Headers(); | |
myHeader.append('Content-Type', 'json/application'); | |
myHeader.append('Authorization', 'Bearer ' + token); | |
let request = new Request(api_url, | |
{ method: 'GET' | |
, headers: myHeader | |
, mode: 'no-cors' | |
}); | |
fetch(request) | |
.then(function (response) { | |
if (!response.ok) { | |
throw response.statusText | |
} | |
return response.json(); | |
}) | |
.then(function (jsondata) { | |
// ensure that downloaded 'data' is available for later use | |
data = jsondata; | |
state2view(jsondata); | |
}) | |
.catch(err => console.log(err)); | |
}; | |
auth_fetch('/api/example', localStorage.jwt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment