为什么axios在使用拦截器时删除请求不起作用?

huangapple go评论87阅读模式
英文:

Why axios delete with interceptors doesnt work?

问题

我正在创建一个简单的删除请求,但我的后端路由从未被触发,我不知道我做错了什么。任何帮助都将很好!!

const instance = axios.create({
    baseURL: 'http://13axxxxx.ngrok.io'
});

instance.interceptors.request.use(
    async (config) => {
        const token = await AsyncStorage.getItem("token");
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    (err) => {
        return Promise.reject(err);
    }
);

export default instance;
import BaseAPI from './BaseAPI';
const deleteAccount = () => {
  return BaseAPI.delete('/delete_account');
}

在我的后端,这个路由从未被触发:

app.use(router.delete('delete_account', (req, res, next) => {
    console.log("delete called")
}))
英文:

I am making a simple delete request but my backend route is never reached and I dont know what I am doing wrong. Any help would be great!!

const instance = axios.create({
    baseURL: 'http://13axxxxx.ngrok.io'
});

instance.interceptors.request.use(
    async (config) => {
        const token = await AsyncStorage.getItem("token");
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    (err) => {
        return Promise.reject(err);
    }
);

export default instance;
import BaseAPI from './BaseAPI';
const delete = () => {
  return BaseAPI.delete('/delete_account');
}

in my backend this route is never hit:

 app.use(router.delete('delete_account', (req, res, next) => {
        console.log("delete called")
    }))

答案1

得分: 1

问题在于您错过了您的代码中起始的“/”:

router.delete("/…")
英文:

The issue here is that you’ve missed the starting “/“ in your

router.delete(“/…”)

huangapple
  • 本文由 发表于 2023年1月8日 02:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75042851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定