0

I want to popup and alert saying "Error Contacting Server" when http request doesn't get any feedback.

.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;

           });
    }])
user6579134
  • 749
  • 3
  • 10
  • 35
  • please check http://stackoverflow.com/questions/27507678/in-angular-http-service-how-can-i-catch-the-status-of-error hope this helps!! – Jomon Raju Sep 19 '16 at 10:20
  • On a side note: "The `$http` legacy promise methods `success` and `error` have been deprecated. Use the standard `then` method instead. If `$httpProvider.useLegacyPromiseExtensions` is set to `false` then these methods will throw `$http/legacy` error." Taken from: https://docs.angularjs.org/api/ng/service/$http – Chris Satchell Sep 19 '16 at 10:21

3 Answers3

1

You should use then instead of success/error methods

Here is your updated code

.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");
           });
        }])
Weedoze
  • 13,683
  • 1
  • 33
  • 63
0

You should specify error callback function.

$http.get('/someUrl', config).then(successCallback, errorCallback);

For more information please see https://docs.angularjs.org/api/ng/service/$http

Sabik
  • 1,599
  • 1
  • 11
  • 10
0

Use promise for it .Set some timeout in your javascript.

var cancelReq= $q.defer();
$http.get('/someUrl', {timeout: cancelReq.promise}).success(successCallback);
$timeout(showErrorPopup, 3000);

after timeout , resolve it and then show popup

function showErrorPopup(){
   cancelReq.resolve(); 
//popup code
} 
Disha
  • 822
  • 1
  • 10
  • 39