Skip to content

Instantly share code, notes, and snippets.

@wsd1
Created October 5, 2016 18:42
Show Gist options
  • Select an option

  • Save wsd1/68e4dff28a09c8d5e5489708ae3be9e3 to your computer and use it in GitHub Desktop.

Select an option

Save wsd1/68e4dff28a09c8d5e5489708ae3be9e3 to your computer and use it in GitHub Desktop.
/*
使用 localStorage 来记录 JWT 认证,以及如何设置 Authorization 头部:
http://stackoverflow.com/questions/35861012/how-to-send-a-token-with-an-ajax-request-from-jquery
*/
$.ajax({
url: "http://localhost:8080/login",
type: 'POST',
data: formData,
error : function(err) {
console.log('Error!', err)
},
success: function(data) {
console.log('Success!')
localStorage.setItem('token', data.id_token);
}
});
$.ajax({
url: "http://localhost:8080/login",
type: 'GET',
// Fetch the stored token from localStorage and set in the header
headers: {"Authorization": localStorage.getItem('token')}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment