Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
  <!DOCTYPE html>
  <html ng-app="app">
  <head>
    <meta name="description" content="ng $http howto access ERR_CONNECTION message" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.6/angular.min.js"></script>
    <meta charset=utf-8 />
    <title>fact-seed</title>
  </head>
  <body ng-controller="MainCtrl">
    test of getData() returning object: {{factoryOutput}}<br><br>
    a server that's down returns no error: {{factoryOutput2}}<br><br>
    a server that's working return object: {{factoryOutput3}}<br><br>
    
    <script>
      angular.module('app', [
      'app.controllers',
      'app.services'
      ]).config(['$httpProvider', function($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
        }
      ]);
      angular.module('app.services',[])
        .factory('myFactory', function($http, $q){
          return {
            getData: function(){
              return {animal: 'dog'}
            },
            isUser: function(name) {
              var url='http://parleyvale.com/isUser/'+name;
              var deferred = $q.defer();
              $http.get(url).   
                success(function(data, status) {
                  deferred.resolve(data);
                }).
                error(function(data, status){
                  deferred.reject({message: 'server is down'})
                });
              return deferred.promise;
            }
          }        
        });
      angular.module('app.controllers',[])
        .controller('MainCtrl', ['$scope', 'myFactory', function($scope, myFactory){
          $scope.factoryOutput=myFactory.getData();  
          $scope.isUser=myFactory.isUser('tim').then(function(data){
            console.log(data.data.items);
            $scope.factoryOutput2=data.data.items
          }, function(data){$scope.factoryOutput2 = data});
        }]);  
    </script>
  </body>
  </html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers