[英]show an alert when http requesht has lasted for x seconds without response with angularjs
[英]popup alert when http request last x seconds with angularjs
当HTTP请求未获得任何反馈时,我想弹出并警告说“ Error Contacting Server”。
.controller('items_ctrl',['$scope','$http',function($scope,$http){
$scope.shop_id=localStorage.getItem("shop_id");
$http.get('http://localhost/myapp/spree/stocks.php').success(function(data){
console.log(JSON.stringify(data));
$scope.item=data;
});
}])
您应该使用then
而不是success/error
方法
这是您更新的代码
.controller('items_ctrl',['$scope','$http',function($scope,$http){
$scope.shop_id=localStorage.getItem("shop_id");
$http.get('http://localhost/myapp/spree/stocks.php').then(function(data){
console.log(JSON.stringify(data));
$scope.item=data;
}, function(error){
alert("Error contacting server");
});
}])
您应该指定错误回调函数。
$http.get('/someUrl', config).then(successCallback, errorCallback);
有关更多信息,请参见https://docs.angularjs.org/api/ng/service/ $ http
为此使用诺言。请在JavaScript中设置一些超时时间。
var cancelReq= $q.defer();
$http.get('/someUrl', {timeout: cancelReq.promise}).success(successCallback);
$timeout(showErrorPopup, 3000);
超时后,解决它,然后显示弹出窗口
function showErrorPopup(){
cancelReq.resolve();
//popup code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.