我在 Angular 应用程序中遇到一个问题,我的 GET 请求被缓存,这不是我想要的行为。这是 Angular 服务方法中获取数据的代码片段:
public getProfileSettings() {
return this.http.get(`${this.configService.Config.api}userprofile/profile-settings`).pipe(
map((response: any) => {
// my code
return response;
})
);
}
在用户配置文件页面的构造函数中,我有这样的东西:
this.clientPanelService.getProfileSettings().subscribe((response) => {
// fetching data from the backend
});
在用户配置文件页面上,我使用 POST 请求更新数据。在导航到另一个页面然后返回到 userprofile 页面后,再次触发 getProfileSettings() 方法,但它返回更新前的旧缓存数据。如何禁用此 GET 请求的缓存?在我将我的 Angular 应用程序从版本 13 更新到 14 后,这个问题开始出现。
尽管我在服务器端 C# 代码中添加了标头 Cache-Control 和 Pragma,但似乎没有帮助。看起来问题可能出在 Angular 方面,因为 Angular 可能还设置了需要覆盖的缓存标头。
编辑我的回复标题:
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, HEAD, DELETE, OPTIONS
Access-Control-Allow-Origin: http://localhost:4200
Cache-Control: no-cache, no-store, must-revalidate, proxy-revalidate
Content-Type: application/json; charset=utf-8
Date: Mon, 10 Apr 2023 20:09:26 GMT
Expires: 0
Pragma: no-cache
Server: Kestrel
Transfer-Encoding: chunked
请求标头:
GET /api/v1/userprofile/profile-settings HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0
Accept: application/json, text/plain, */*
Accept-Language: en
Accept-Encoding: gzip, deflate, br
Origin: http://localhost:4200
Connection: keep-alive
Referer: http://localhost:4200/
Cookie: csy_uu=d86d152f-179e-48cf-80b8-b1fe261a659e; csy_ba=1; csy_n=1; currentURL=%2F; csy_ac=9a5d90c8d04c4ad5b8023a4d4f01c97c; csy_re=17af9ff19d4a46f889fd5d762092798f; csy_ac_ex=1681157444773
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
Pragma: no-cache
Cache-Control: no-cache
这个问题是在将 Angular 从版本 13 更新到 14 后发生的。如果我将此更改恢复到版本 13,一切都会按预期工作。