AngularJS 返回缓存数据的 HTTP GET 请求

AngularJS HTTP GET request returning cached data

在我的 AngularJS 应用程序中,我正在发送 HTTP GET 请求,如下所示。

MyService.HttpReq("testUrl", "GET", null);

HttpReq方法在服务中定义并实现如下:

this.HttpReq = function(URL, method, payload)
{
    $http({
        url: URL,
        method: method,
        cache: false,
        data: postData,
        headers: {
            'Content-Type': 'application/json',
        }
    }).success(function(response)
    {
        console.log("Success: "+JSON.stringify(response));

    }).error(function(data, status)
    {
       console.error("Error");

    });
}

首先,这是在 AngularJS 中发送 HTTP 请求的正确方法吗?
我面临的问题是,有时我得到缓存数据作为响应,而 HTTP 请求没有到达服务器。可能是什么问题?

更新

根据评论和回答,我更新了我的 HTTP 请求代码如下,但仍然遇到同样的问题。

this.HttpReq = function(URL, method, payload)
{
    $http({
        url: URL, 
        method: method, 
        cache: false,
        data: payload,
        headers: {
            'Content-Type': 'application/json',
            'Cache-Control' : 'no-cache'
        }
    }).
    then(
        function(response) 
        {
            var data = response.data;
            console.log("Success: "+JSON.stringify(data));
        }, 
        function(response) 
        {
            var data = response.data || "Request failed";
            var status = response.status;
            console.error("Error: "+JSON.stringify(data));
        }
    );
}

只需将 cache: false 属性添加到配置 object.

https://docs.angularjs.org/api/ng/service/$http#caching

您还可以添加 header: 'Cache-Control' : 'no-cache'

IE 浏览器将捕获 ajax get 请求,即使我们将缓存控制 headers 添加到响应中也是如此。我发现解决问题的唯一方法是向请求添加一些随机参数。请确保 api 没有问题,即使您发送额外的参数

 MyService.HttpReq("testUrl?ts=" + Date.now(), "GET", null);