简体   繁体   English

如何在每个请求中添加拦截器以保持请求,即不缓存请求

[英]How to add an interceptor to each request to keep ie from caching requests

Internet explorer caches http requests. Internet Explorer缓存http请求。 Instead of manually adding a header to each individual function I want to do something like this 与其将标题手动添加到每个单独的函数中,我不希望这样做

axios.interceptors.request.use((config): AxiosRequestConfig => {
return addNoCachingHeader(config);
  });

and

const addNoCachingHeader = (config: AxiosRequestConfig): AxiosRequestConfig => {
  return { ...config, headers: { ...config.headers, Pragma: "no-cache"} };
};

is there a simple way to keep IE from caching requests without going back through my whole app and adding headers to each individual request? 有没有一种简单的方法可以防止IE缓存请求,而无需返回整个应用程序并向每个单独的请求添加标头?

I think using a generic client approach would be better here. 我认为在这里使用通用客户端方法会更好。

const client = axios.create({
  baseURL,
  timeout: 5000,
  responseType: 'json',
  headers: { Pragma: "no-cache" },
});

And to use in other places import client and call client.get or client.post 并在其他地方使用导入客户端并调用client.getclient.post

if you want to override headers at some point, make this to a function 如果您想在某个时候覆盖标题,请将其设为函数

const client = (headers) => axios.create({
  baseURL,
  timeout: 5000,
  responseType: 'json',
  headers,
});

and use as client({ Pragma: 'no-cache' }).get(...) 并用作client({ Pragma: 'no-cache' }).get(...)

创建一个您希望所有请求都使用的标头的中间件,然后让您的应用程序通过app.use(yourHeaderMiddleware(req,res,next))使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM