跳到主要内容

2 篇博文 含有标签「axios」

查看所有标签

swr取消请求hook封装

· 阅读需 1 分钟
TransonQ
Front-end Engineer
function useCancellableSWR(key, swrOptions) {
const source = axios.CancelToken.source();

return [
useSWR(key, (url) => axios.get(url, { cancelToken: source.token }).then(res => res.data), {
...swrOptions,
}),
source,
];
}

// usage:
const [{ data }, cancelFn] = useCancellableSWR('/endpoint');

cancelFn.cancel()

issue-129-comment

axios的post请求没有参数的情况不会携带请求头

· 阅读需 1 分钟
TransonQ
Front-end Engineer

联调问题​

go 后端框架 Kratos 如果收到没有携带请求头 content-type, 框架会报 400, 错误信息是 unregistered xxx :content-type

axios (0.27.2)​

如果 post 请求没有传任何参数的情况下, 默认会去掉请求头 content-type, 请求拦截器加上也会被清除

最快的解决方法​

请求时添加一个空对象即可

export const postTest = () => ax.post(`/test/url/example`, {})