AngularJS HTTP GET请求返回缓存数据

前端之家收集整理的这篇文章主要介绍了AngularJS HTTP GET请求返回缓存数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的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请求没有到达服务器.可能是什么问题?

UPDATE

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

this.HttpReq = function(URL,data: payload,'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属性添加到配置对象即可.

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

您还可以添加标题:’Cache-Control’:’no-cache’

猜你在找的Angularjs相关文章