Use*_*337 5 http-get angularjs
在我的 AngularJS 应用程序中,我发送 HTTP GET 请求,如下所示。
MyService.HttpReq("testUrl", "GET", null);
Run Code Online (Sandbox Code Playgroud)
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");
});
}
Run Code Online (Sandbox Code Playgroud)
首先,这是在 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));
}
);
}
Run Code Online (Sandbox Code Playgroud)
只需将cache: false属性添加到配置对象即可。
https://docs.angularjs.org/api/ng/service/$http#caching
您还可以添加标头: 'Cache-Control' : 'no-cache'
| 归档时间: |
|
| 查看次数: |
4653 次 |
| 最近记录: |