2014-10-03 84 views
1

我想创建一个angularjs应用程序作为cbssports插件。AngularJS访问Cbssports REST API

它们提供了一个RESTful API。

下面是我的$ http请求:

$http.get(basePath + 'league/owners?access_token=' + cbssportsTokens['access_token'] + '&response_format=JSON') 
     .success(function(data) { 
      return data; 
     }); 

即在工厂包裹起来,并在控制器中使用。据我所知,这是正确执行。当我看着Chrome开发人员工具,我可以看到下面的错误在控制台中我的要求:

OPTIONS http://api.cbssports.com/fantasy/league/owners?access_token=U2FsdGVkX18Hyd0…J9DrpO7C-OXQQNXGMh0ej0iXVfPf5DkQwkLwSpCqGhipd6HogV_gZ&response_format=JSON angular.js:8560 
(anonymous function) angular.js:8560 
sendReq angular.js:8354 
$http.serverRequest angular.js:8087 
wrappedCallback angular.js:11572 
wrappedCallback angular.js:11572 
(anonymous function) angular.js:11658 
Scope.$eval angular.js:12701 
Scope.$digest angular.js:12513 
Scope.$apply angular.js:12805 
done angular.js:8378 
completeRequest angular.js:8592 
xhr.onreadystatechange 
XMLHttpRequest cannot load http://api.cbssports.com/fantasy/league/owners?access_token=U2FsdGVkX18xBod…oQWvjDVSbpZCOVsoIKeVXKRSYdo6dBbIE0rgMWTkWhmgPUTyr_xnS&response_format=JSON. The 'Access-Control-Allow-Origin' header has a value 'https://www.cbssports.com' that is not equal to the supplied origin. Origin 'http://xx.xx.xx.xx:9001' is therefore not allowed access. ?access_token=U2FsdGVkX18xBodWEOfeqys5X4aDpghYrE22FGljlJd_TtKRHlWh4LHWFwVxay95BbAWvn4te1foQWvjDVSbp…:1 

当我点击的链接,它说,它无法加载,它带我到一个新的页面预期输出!所以CORS应该不是服务器的问题。

我已经阅读了许多关于更改标题的CORS的不同问题。该遵循的是我如何设定它:

$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*'; 
    $httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = 'Origin'; 

任何意见,将不胜感激,也是我的第一篇来左右,所以请让我知道如果有更多的信息将有助于。

+0

我使用的全部是'$ httpProvider.defaults.useXDomain = true;'和'delete $ httpProvider.defaults.headers.common ['X-Requested-With'];'使用CORS。虽然,我正在使用$资源。 – Owen 2014-10-03 21:54:01

+0

我也试过,结果相同。 – rbuckley 2014-10-03 22:28:51

回答

0

所以我没有完全明白它的工作。但我找到了一种解决方法。

function _get(url) { 
    return $resource(basePath + url, { 
      access_token: cbssportsTokens['access_token'], 
      response_format: 'JSON', 
      callback: 'JSON_CALLBACK' 
    }, { 
     get: { 
      method: 'JSONP' 
     } 
    }); 
    } 

我继续使用JSONP请求而不是GET,cbs服务器似乎更快乐。