2024.08.09今天我学习了fetch向服务器发送请求,并且携带token,代码如下:
1.get请求
const token = store.getters.token;
const url = 'xxxxxxxxxx';
fetch(url,{
method:'get',
headers:{
'Authorization':`Bearer ${token}`,
'Content-Type': 'application/json'
}
})
2.post请求
const token = store.getters.token;
const url = 'xxxxxxxxxx';
const data = {name:'xxx'}
fetch(url,{
method:'post',
headers:{
'Authorization':`Bearer ${token}`,
'Content-Type': 'application/json'
},
body:JSON.stringify(data)
})