我正在尝试通过使用Twitch API(https://github.com/justintv/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无法加载https://api.twitch.tv/kraken。该请求已重定向到“ http://www.twitch.tv/kraken ”,对于需要预检的跨域请求是不允许的。
我知道我需要使用JSONP请求,但是如何设置标头呢?谁能给我展示一个JSONP请求的示例以及如何像上面的示例中那样设置标头?如果无法在JSONP请求中设置标头,还应如何设置请求标头?
通过以下方式提出请求:
$http.jsonp('https://api.twitch.tv/kraken/games/top?limit=' + number + '&callback=JSON_CALLBACK')
这将返回给您json,并且XMLHttpRequest不会有问题
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句