Created
October 5, 2016 18:42
-
-
Save wsd1/68e4dff28a09c8d5e5489708ae3be9e3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 认证,以及如何设置 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