Angular $http 与 bbc 电台 api
Angular $http with bbc radio api
我需要从 bbc 电台得到这个 json:
http://www.bbc.co.uk/radio1/playlist.json
我可以从 POSTMAN 获取它,但后来我尝试从我的 angular 应用程序获取它,但出现错误。
XMLHttpRequest cannot load http://www.bbc.co.uk/radio1/playlist.json. No 'Access-Control-Allow-Origin' header is present on the requested resource
是的,我在 Stack Overflow 上看到了很多类似的问题,但我认为这不是要解决的服务器任务。
在我的 angular 代码中我得到了这个:
async: function (radio) {
return $http({
method: 'GET', //or JSON
url: api.url + radio + '/playlist.json',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*'
}
})
.success(function (d) {
console.log('done');
})
.error(function (d) {
console.log('nope');
});
}
这在配置中:
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
但 CORS 无论如何都不起作用。
显然这个播放列表和 BBC api 不支持 jsonp 并且没有 CORS 设置所以我可以从头到尾想到的解决方案是使用即 CURL
http://plnkr.co/edit/66yErNbEkONrcC8LjqUV?p=preview
$http.jsonp('http://edeen.pl/curl.php?callback=JSON_CALLBACK').then(function(response){
$scope.playlist = response.data
console.log(response.data)
})
curl.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.bbc.co.uk/radio1/playlist.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $_GET['callback'].'('.$output.')';
我需要从 bbc 电台得到这个 json: http://www.bbc.co.uk/radio1/playlist.json
我可以从 POSTMAN 获取它,但后来我尝试从我的 angular 应用程序获取它,但出现错误。
XMLHttpRequest cannot load http://www.bbc.co.uk/radio1/playlist.json. No 'Access-Control-Allow-Origin' header is present on the requested resource
是的,我在 Stack Overflow 上看到了很多类似的问题,但我认为这不是要解决的服务器任务。
在我的 angular 代码中我得到了这个:
async: function (radio) {
return $http({
method: 'GET', //or JSON
url: api.url + radio + '/playlist.json',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*'
}
})
.success(function (d) {
console.log('done');
})
.error(function (d) {
console.log('nope');
});
}
这在配置中:
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
但 CORS 无论如何都不起作用。
显然这个播放列表和 BBC api 不支持 jsonp 并且没有 CORS 设置所以我可以从头到尾想到的解决方案是使用即 CURL
http://plnkr.co/edit/66yErNbEkONrcC8LjqUV?p=preview
$http.jsonp('http://edeen.pl/curl.php?callback=JSON_CALLBACK').then(function(response){
$scope.playlist = response.data
console.log(response.data)
})
curl.php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.bbc.co.uk/radio1/playlist.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $_GET['callback'].'('.$output.')';