2

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!

2
  • 1
    seems like 1st link is enabled with CORS. whether as the other one doesn't have enabled CORS.. you need to unable CORS on server side of 2nd link Commented Apr 21, 2016 at 16:23
  • Cross-Origin Requests (CORS) - Enable it Commented Apr 21, 2016 at 16:28

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I was using Node-Red on my other site. I found out how to enable CORS and it's working now, thanks! For other sites where I was having this problem, I assume there isn't a way around this if I don't have access to configure CORS on those servers?
You can use CURL, as you said.
0

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;
  });
}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.