在 axios post 方法中获取错误

Getting errors in axios post method

我根本无法理解这段代码有什么问题。 我正在尝试发出 post 请求以使用 axios 从 API 注销。 我一直收到错误 401,我认为这是因为请求没有使用正确的代码,但我已经尝试使用授权、承载、令牌等,但没有任何效果。

const handleExit = () => {
    const tokenAtual = sessionStorage.getItem("token");
    
    console.log(tokenAtual)

    axios.post("https://api.secureme.pt/api/v1/auth/logout", {
        headers: {
            'Authorization': `token ${tokenAtual}`
          }
    }).then(response =>{
        setLoading(false);
        console.log("response.data.message>>>>>>>>>>>" , response.data.message);
        navigate('/')
    }).catch(error => {
        console.log("error -> ", error);
        setLoading(false);
        if(error.response.status === 406){
            setError(error.response.data.message);
        }
        else {
            setError("Something went wrong, please try again later.");
        }
        console.log("error -> ", error);
    });
    

}

------------邮递员代码------------

curl --location --request POST 'https://api.secureme.pt/api/v1/auth/logout' \
--header 'Authorization: ----tokenAtual----'

你的请求的问题是 syntax/params 你发送给 post 请求它应该像

axios.post(
"https://api.secureme.pt/api/v1/auth/logout",
{},
{
headers:{
'Authorization': `token ${tokenAtual}`
}
})

post 请求的 syntax/param 是

(url: string, data?: any, config?: AxiosRequestConfig | undefined)

所以您正在尝试发送配置来代替数据

axios.post方法中:-

axios.post(url[, data[, config]])

第一个参数是url。第二个参数是data,第三个参数是config。您正在传递 config 代替 data

固定格式如下:

axios.post('https://api.secureme.pt/api/v1/auth/logout', undefined, {headers: {
  'Authorization': tokenAtual
}})