Jeremy
Jeremy

Reputation: 21

AngularJS HTTP Get not getting a response

I'm having trouble getting responses from certain sites using http.get. Every get I do from Firebase seems to work, but many other sites do not, although the requests work fine from my browser or curl. I have two URL's that provide the exact same data, one from Firebase and another from a Cloud9 project. The Firebase one works and the c9 one doesn't.

When it doesn't work I get the following response:

{"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://dl-celeb-dp-x2jroy.c9users.io:8081/getScoreboard","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}

I've tried HTTP as well as HTTPS for the one that isn't working but I get the same response either way.

I have a fiddle to demonstrate: https://jsfiddle.net/9d7mysns/

HTML:

<h1>{{result}}</h1>
<p>{{content}}</p>

</div>

Javascript:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("https://blazing-torch-1276.firebaseio.com/getScoreboard.json") //Working
//$http.get("https://dl-celeb-dp-x2jroy.c9users.io:8081/getScoreboard") //Not Working
  .then(function(response) {
      $scope.result = "Success";
      $scope.content = response;
  }, function(response) {
      $scope.result = "Error";
      $scope.content = response;
  });
});

Please help, Thanks!

Upvotes: 2

Views: 2067

Answers (2)

Mast
Mast

Reputation: 1

Try this:

var app = angular.module('myApp', []);
app.controller('myCtrl',['$scope', '$http', function($scope, $http) {
  //$http.get("https://blazing-torch-1276.firebaseio.com/getScoreboard.json") //Working
  $http.get('https://dl-celeb-dp-x2jroy.c9users.io:8081/getScoreboard',{
     header : {'Content-Type' : 'application/json; charset=UTF-8'}
  }) //Now Working
  .success(function(response) {
    $scope.result = "Success";
    $scope.content = response;
  }).error(function(response) {
    $scope.result = "Error";
    $scope.content = response;
  });
}]);

Upvotes: 0

Bhabishya Kumar
Bhabishya Kumar

Reputation: 731

I think the second one is not allowing Cross-Domain request. This is what I see in the Chrome console for your fiddle.

XMLHttpRequest cannot load https://dl-celeb-dp-x2jroy.c9users.io:8081/getScoreboard. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fiddle.jshell.net' is therefore not allowed access.

Upvotes: 5

Related Questions