使用$http服务的AngularJS跨域请求

使用$http服务的AngularJS跨域请求,angularjs,rest,http-headers,jsonp,cross-domain-policy,Angularjs,Rest,Http Headers,Jsonp,Cross Domain Policy,我试图通过使用Twitch API()制作一个简单的web应用程序来学习AngularJS,但是我在执行GET请求时遇到了问题,因为它是一个跨域请求。这就是我尝试过的 angular.module('streamPlaylisterAppServices', []).factory('twitchService', ['$http', function($http) { return function(usercode){ console.log("usercode in

我试图通过使用Twitch API()制作一个简单的web应用程序来学习AngularJS,但是我在执行GET请求时遇到了问题,因为它是一个跨域请求。这就是我尝试过的

angular.module('streamPlaylisterAppServices', []).factory('twitchService', ['$http',
  function($http) {
    return function(usercode){
      console.log("usercode inside service is: " + usercode)
      var authHeader = 'OAuth' + usercode;

      return $http({
        method: 'GET',
        url: ' https://api.twitch.tv/kraken',
        cache: false,
        headers:{
          'Accept': 'application/vnd.twitchtv.v3+json',
          'Authorization': 'OAuth ' + usercode
        }
      })
    };
  }]);
但我得到了一个错误:

无法加载XMLHttpRequest。请求 已重定向到“”,这是不允许的 用于需要飞行前的跨来源请求

我知道我需要使用一个JSONP请求,但是如何设置这样的头呢?有人能给我展示一个JSONP请求的例子,以及如何像我在上面的例子中那样为它设置头吗?如果我不能在JSONP请求中设置头,那么我该如何设置请求头?

使用以下命令进行请求:

$http.jsonp('https://api.twitch.tv/kraken/games/top?limit=' + number + '&callback=JSON_CALLBACK')
这将返回给您json,您不会遇到XMLHttpRequest问题