Angular IE Caching issue for $http

2019-01-01 03:07发布

All the ajax calls that are sent from the IE are cached by Angular and I get a 304 response for all the subsequent calls . Though the request is the same, the response is not gonna be the same in my case. I wanna disable this cache. I tried adding the cache attribute to $http.get but still it didnt help. How can this issue be resolved?

17条回答
路过你的时光
2楼-- · 2019-01-01 03:30

Always use a simple approach add Timestamp with each request no need to clear cache

    let c=new Date().getTime();
    $http.get('url?d='+c)
查看更多
临风纵饮
3楼-- · 2019-01-01 03:32
meta http-equiv="Cache-Control" content="no-cache"

I just added this to View and it started working on IE. Confirmed to work on Angular 2.

查看更多
回忆,回不去的记忆
4楼-- · 2019-01-01 03:34

also you can try in your servce to set headers like for example:

...
import { Injectable } from "@angular/core";
import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http";
...
 @Injectable()
export class MyService {

    private headers: HttpHeaders;


    constructor(private http: HttpClient..) 
    {


        this.headers = new HttpHeaders()
                    .append("Content-Type", "application/json")
                    .append("Accept", "application/json")
                    .append("LanguageCulture", this.headersLanguage)
                    .append("Cache-Control", "no-cache")
                    .append("Pragma", "no-cache")                   
    }
}
....
查看更多
荒废的爱情
5楼-- · 2019-01-01 03:38

I have found better solution: Better Way to Prevent IE Cache in AngularJS?

For lazy ones here is a solution:

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult Get()
{
    // return your response
}
查看更多
浪荡孟婆
6楼-- · 2019-01-01 03:39

The guaranteed one that I had working was something along these lines:

myModule.config(['$httpProvider', function($httpProvider) {
    if (!$httpProvider.defaults.headers.common) {
        $httpProvider.defaults.headers.common = {};
    }
    $httpProvider.defaults.headers.common["Cache-Control"] = "no-cache";
    $httpProvider.defaults.headers.common.Pragma = "no-cache";
    $httpProvider.defaults.headers.common["If-Modified-Since"] = "Mon, 26 Jul 1997 05:00:00 GMT";
}]);

I had to merge 2 of the above solutions in order to guarantee the correct usage for all methods, but you can replace common with get or other method i.e. put, post, delete to make this work for different cases.

查看更多
登录 后发表回答