vue使用 axios,get请求、post 请求头部添加token

post:

const res = await axios.post(url, 参数, { headers });
例如:
export async function apireportheader(params, headers) {
  const res = await axios.post('/dmp/openApi/apireportheader', params, { headers });
  if (res.status === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}
语法:

let params = {
//请求参数设置
}
axios.post(url,params,{
headers:{
'token':localStorage.getItem("token")
}
}).then(res=>{
console.log('res=>',res)
})

get:

const res = await axios.get(`/xxx/${params.objectid}/xxx`, { headers: headers });
例如:
export async function multipleSynchronizations(params, headers) {
  const res = await axios.get(`/dmp-offlineComputation/offlineComputation/dataflow/${params.objectid}/execute`, { headers: headers });
  if (res.status === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}
语法:

let args = {
//请求参数设置
}
axios.get(url,{
headers:{
'token':localStorage.getItem("token")
},
params:args
}).then(res=>{
console.log('res=>',res)
})

posted @ 2024-11-21 16:43  Ao_min  阅读(16)  评论(0编辑  收藏  举报