angularjs – 以角度$http确定请求超时

前端之家收集整理的这篇文章主要介绍了angularjs – 以角度$http确定请求超时前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我发送一个http请求使用角度如下。
$http({
    url: url,params: params,method:'POST',headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    },timeout: 60000    //60 seconds
}).success(function(data,status,headers,config) {
    //do something 
}).error(function(data,header,config) {
    if(timedout){  //determine occurrence of timeout.
        //invoke timeout handler
    } else  
        //handle other error
    }
});

如何确定超时?

我已经观察到在这种情况下接收到状态码“0”。检查状态== 0是否安全?

请注意,我不是要求HTTP请求超时(状态码408)。

我已经做了如下…
var startTime = new Date().getTime();
$http.post(...)
    .success(function(resp,config) {...})
    .error(function(resp,config) {
        var respTime = new Date().getTime() - startTime;
        if(respTime >= config.timeout){
            //time out handeling
        } else{
            //other error hanndling
        }
    });

猜你在找的Angularjs相关文章