前端如何在header夹带token

  • jquery写法1:

直接post

1
2
3
4
5
6
$.ajax({
url: "http://localhost:8080/login",
type: 'GET',
// Fetch the stored token from localStorage and set in the header
headers: {"Authorization": localStorage.getItem('token')}
});
  • jquery写法2:
    从localStorage中捞出保存好的token,然后全局设置夹带
    1
    2
    3
    4
    5
    6
    7
    8
    9
    var token = window.localStorage.getItem('token');

    if (token) {
    $.ajaxSetup({
    headers: {
    'x-access-token': token
    }
    });
    }

商务洽谈