AngularJS HTTP Get 没有得到响应

AngularJS HTTP Get not getting a response

本文关键字:响应 HTTP Get AngularJS      更新时间:2023-09-26

我在使用 http.get 从某些网站获取响应时遇到问题。 我从Firebase所做的每一次获取似乎都有效,但许多其他网站却不起作用,尽管这些请求在我的浏览器或curl上工作正常。 我有两个网址提供完全相同的数据,一个来自Firebase,另一个来自Cloud9项目。 Firebase一个有效,c9一个不工作。

当它不起作用时,我得到以下响应:

{"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":""}

我已经尝试了HTTP和HTTPS来对付不起作用的HTTPS,但无论哪种方式,我都会得到相同的响应。

我有一个小提琴要演示: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;
  });
});

请帮忙,谢谢!

我认为第二个是不允许跨域请求。这是我在Chrome控制台中看到的小提琴。

XMLHttpRequest 无法加载 https://dl-celeb-dp-x2jroy.c9users.io:8081/getScoreboard。对预检请求的响应未通过访问控制检查:请求的资源上不存在"访问控制允许源"标头。因此,不允许访问源"https://fiddle.jshell.net"。

试试这个:

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